2017-03-03 210 views
0

我從CSV導入的陣列。陣列看起來像這樣添加陣列陣列PHP

Array 
(
    [0] => Array 
     (
      [DOCategory] => IM 
      [ToCustomerCode] => M-CBC 
      [ExternalDocNo] => DRDOC-DJ-GBAR10-25 
      [Item] => S160373 
      [Quantity] => 4 
      [Remark2] => TEST 
     ) 

    [1] => Array 
     (
      [DOCategory] => IM 
      [ToCustomerCode] => M-CBD 
      [ExternalDocNo] => DRDOC-DJ-GBAR10-25 
      [Item] => S160373 
      [Quantity] => 5 
      [Remark2] => TEST2 
     ) 

    [2] => Array 
     (
      [DOCategory] => IM 
      [ToCustomerCode] => M-CJ 
      [ExternalDocNo] => DRDOC-DJ-GBAR10-25 
      [Item] => S160373 
      [Quantity] => 5 
      [Remark2] => TEST3 
     ) 

    [3] => Array 
     (
      [DOCategory] => IM 
      [ToCustomerCode] => M-CL 
      [ExternalDocNo] => DRDOC-DJ-GBAR10-25 
      [Item] => S160373 
      [Quantity] => 4 
      [Remark2] => TEST4 
     ) 

    [4] => Array 
     (
      [DOCategory] => IM 
      [ToCustomerCode] => M-CRG 
      [ExternalDocNo] => DRDOC-DJ-GBAR10-25 
      [Item] => S160373 
      [Quantity] => 4 
      [Remark2] => TEST5 
     ) 

    [5] => Array 
     (
      [DOCategory] => IM 
      [ToCustomerCode] => M-CRGM 
      [ExternalDocNo] => DRDOC-DJ-GBAR10-25 
      [Item] => S160373 
      [Quantity] => 4 
      [Remark2] => TEST6 
     ) 

    [6] => Array 
     (
      [DOCategory] => IM 
      [ToCustomerCode] => M-CRSB 
      [ExternalDocNo] => DRDOC-DJ-GBAR10-25 
      [Item] => S160373 
      [Quantity] => 4 
      [Remark2] => TEST7 
     ) 

) 

但現在我想添加一個會話。所以我的數組會看起來像這樣。

[0] => Array 
     (
      [DOCategory] => IM 
      [ToCustomerCode] => M-CBC 
      [ExternalDocNo] => DRDOC-DJ-GBAR10-25 
      [Item] => S160373 
      [Quantity] => 4 
      [Remark2] => TEST 
      [session] => .... 
     ) 

[session]從這個

$random = md5(uniqid($userlogin, true)); 
$new = array('session' => $random); 

那麼這裏是我到目前爲止

$x=0; 
        while(($line = fgetcsv($f)) !== false) { 
         $data[] = $line; 
        } 
        for($x=0;$x<count($data);$x++){ 
         if($x != 0) { 
          $newArray[] = array_combine($data[0], $data[$x]); 
          $newArrays[] = $new; 
         } 
        } 
       $newArray = array_push($newArray,$newArrays); 
       echo "<pre>";print_r($newArray); 

試圖從我echo "<pre>";print_r($newArray);結果是8。所以我怎麼能達到我想要的。預先感謝並對我的英語不好。

+1

爲什麼不只是使用for或foreach語句遍歷數組並添加會話密鑰/值? – pteronewone

回答

1

您的代碼很奇怪,但試試這個:

$x=0; 
while(($line = fgetcsv($f)) !== false) { 
    $data[] = $line; 
} 
for($x=0;$x<count($data);$x++){ 
    if($x != 0) { 
     $newArray[] = array_combine($data[0], $data[$x]); 
     $newArray[$x]["session"] = $new; 
    } 
} 
echo "<pre>";print_r($newArray); 

你也可以跳過臨時數組,如果你不需要它:

$x=0; 
while(($line = fgetcsv($f)) !== false) { 
    $data[] = $line; 
} 
for($x=0;$x<count($data);$x++){ 
    if($x != 0) { 
     $data[$x]["session"] = $new; 
    } 
} 
echo "<pre>";print_r($data); 
+0

我仍然需要這部分'$ newArray [] = array_combine($數據[0],$數據[$ X]);' – YVS1102

+0

確定,所以只在陣列添加到它的下一行。 '$ newArray [$ x] [「session」] = $ new;' – gbolo

+0

@ YVS1102如果它對你有用,請接受答案... – gbolo

1

您可以使用此功能

$arr1= array_merge($arr1, $arr2);

+0

它像這樣['] =>數組 ( [DOCategory] => IM [ToCustomerCode] => M-華潤雪花 [ExternalDocNo] => DRDOC-DJ-GBAR10-25 [項目] => S160373 [數量] => 4 [備註2] => TEST7 ) [會議] => e3ba1d241080830f2638be5a91debd69' – YVS1102

+0

請說更清楚的麻煩,我爲使用移動網絡,所以我不能清楚地看到 –

0

循環在你的陣列,並添加一個會話指數

for($x=0;$x<count($data);$x++) 
    { 
    $newArrays[$x]['session'] = $new; 
    } 
+0

我回答了這個第一:( – gbolo

0

您可以使用array_walk

試試這個

array_walk($array, function(&$a) { 
    $a['session'] = md5(uniqid($userlogin, true)); 
}); 
0

使用array_map()的foreach或array_walk到itereate並添加會話密鑰。

array_map(function($v){ 
    $v['session'] = md5(uniqid($userlogin, true)); 
    return $v; 
});