2011-12-14 94 views
0

好了,所以這是我的第一個數組:PHP array_merge沒有做什麼我需要做的

(
[1] => Array 
    (
     [27] => Array 
      (
       [product_id] => 27 
       [type] => hardware 
       [step_number] => 1 

      ) 

     [372] => Array 
      (
       [product_id] => 372 
       [type] => hardware 
       [step_number] => 1 

      ) 

     [92] => Array 
      (
       [product_id] => 92 
       [type] => hardware 
       [step_number] => 1 
      ) 

    ) 

[2] => Array 
    (
     [335] => Array 
      (
       [product_id] => 335 
       [type] => hardware 
       [step_number] => 2 

      ) 

     [62] => Array 
      (
       [product_id] => 62 
       [type] => hardware 
       [step_number] => 2 
      ) 

     [356] => Array 
      (
       [product_id] => 356 
       [type] => hardware 
       [step_number] => 2 
      ) 

    ) 

,這裏是我的第二個陣列

(
[1] => Array 
    (
     [655] => Array 
      (
       [product_id] => 655 
       [type] => optional 
       [step_number] => 1 
      ) 

     [54] => Array 
      (
       [product_id] => 54 
       [type] => optional 
       [step_number] => 1 
      ) 

     [554] => Array 
      (
       [product_id] => 554 
       [type] => optional 
       [step_number] => 1 
      ) 
    ) 

[2] => Array 
    (
     [33] => Array 
      (
       [product_id] => 33 
       [type] => optional 
       [step_number] => 2 
      ) 
     [612] => Array 
      (
       [product_id] => 612 
       [type] => optional 
       [step_number] => 2 
      ) 
     [5] => Array 
      (
       [product_id] => 5 
       [type] => optional 
       [step_number] => 2 
      ) 
    ) 

[3] => Array 
      (
       [444] => Array 
        (
         [product_id] => 444 
         [type] => optional 
         [step_number] => 3 
        ) 
       [6] => Array 
        (
         [product_id] => 6 
         [type] => optional 
         [step_number] => 3 
        ) 
       [53] => Array 
        (
         [product_id] => 53 
         [type] => optional 
         [step_number] => 3 
        ) 
      ) 

基本上我需要的是第二陣列附加到第一個陣列的末尾,第一個陣列的鍵保留了鍵並將step_number更改爲新鍵,因此最終鍵看起來像

(
[1] => Array 
(
     [27] => Array 
      (
       [step_number] => 1) 
.... 
[2] => Array 
(
     [335] => Array 
      (
       [step_number] => 2) 
.... 
[3] => Array 
(
     [655] => Array 
      (
       [step_number] => 3) 
.... 
[4] => Array 
(
     [33] => Array 
      (
       [step_number] => 4) 
.... 
[5] => Array 
(
     [444] => Array 
      (
       [step_number] => 5) 
.... 

但是當我做

$all = array_merge($first, $second); 

鍵是

(
[0] => Array 
[1] => Array 
[2] => Array 
[3] => Array 
[4] => Array 

這是可能做到....

+0

處理您的答案。 – 2011-12-14 22:50:31

+0

你在「保存」這個詞後失去了我。 – goat 2011-12-14 22:58:43

回答

0

這也是從如果你只是想把你的第二個數組添加到第一個數組(http://php.net/manual/en/function.array-push.php),你可以在array_splice上手動:

array_splice($first, count($first), 0, $second); 
0

迭代第一組鍵,然後使用Union array operator應該做的伎倆。

//$first = array(...); 
//$second = array(...); 

foreach ($second as $key => $value) 
{ 
    if (!isset($first[$key])) 
    { 
     $first[$key] = array(); 
    } 

    $first[$key] += $value; 
}