2011-03-03 38 views
0

有沒有一個好的功能可以按鍵組合? (在這個例子是PID)數組在數組中通過數組鍵?

array 
    0 => 
    array 
     'product' => string 'a product pid 3' (length=9) 
     'name' => string 'adamramadhan' (length=12) 
     'pid' => string '3' (length=1) 
     'timecreate' => string '2011-02-26 13:30:07' (length=19) 
    1 => 
    array 
     'product' => string 'a product pid 4' (length=8) 
     'name' => string 'adamramadhan' (length=12) 
     'pid' => string '4' (length=1) 
     'timecreate' => string '2011-02-26 13:30:54' (length=19) 

array 
    0 => 
    array 
     'pid' => string '3' (length=1) 
     'comment' => string 'a comment on pid 3' (length=8) 
    1 => 
    array 
     'pid' => string '4' (length=1) 
     'comment' => string 'a comment on pid 4' (length=8) 
    2 => 
    array 
     'pid' => string '3' (length=1) 
     'comment' => string 'a comment on pid 3' (length=5) 
    3 => 
    array 
     'pid' => string '4' (length=1) 
     'comment' => string 'a comment on pid 4' (length=5) 
    4 => 
    array 
     'pid' => string '3' (length=1) 
     'comment' => string 'a comment on pid 3' (length=7) 
    5 => 
    array 
     'pid' => string '4' (length=1) 
     'comment' => string 'a comment on pid 4' (length=7) 
    6 => 
    array 
     'pid' => string '3' (length=1) 
     'comment' => string 'a comment on pid 3' (length=18) 
    7 => 
    array 
     'pid' => string '4' (length=1) 
     'comment' => string 'a comment on pid 4' (length=18) 

爲類似

array 
    0 => 
    array 
     'product' => string 'a product pid 3' (length=9) 
     'name' => string 'adamramadhan' (length=12) 
     'pid' => string '3' (length=1) 
     'timecreate' => string '2011-02-26 13:30:07' (length=19) 
     'comments' => array 
     0 => 
      array 
      'pid' => string '3' (length=1) 
      'comment' => string 'a comment on pid 3' (length=8) 
     2 => 
      array 
      'pid' => string '3' (length=1) 
      'comment' => string 'a comment on pid 3' (length=5) 
     4 => 
      array 
      'pid' => string '3' (length=1) 
      'comment' => string 'a comment on pid 3' (length=7) 
     6 => 
      array 
      'pid' => string '3' (length=1) 
      'comment' => string 'a comment on pid 3' (length=18) 
    1 => 
    array 
     'product' => string 'a product pid 4' (length=8) 
     'name' => string 'adamramadhan' (length=12) 
     'pid' => string '4' (length=1) 
     'timecreate' => string '2011-02-26 13:30:54' (length=19) 
     'comments' => array 
     1 => 
      array 
      'pid' => string '4' (length=1) 
      'comment' => string 'a comment on pid 4' (length=8) 
     3 => 
      array 
      'pid' => string '4' (length=1) 
      'comment' => string 'a comment on pid 4' (length=5) 
     5 => 
      array 
      'pid' => string '4' (length=1) 
      'comment' => string 'a comment on pid 4' (length=7) 
     7 => 
      array 
      'pid' => string '4' (length=1) 
      'comment' => string 'a comment on pid 4' (length=18) 

或與每個產品的評論陣列上沒有PID(嵌套上的產品陣列)?

感謝拜訪。

亞當·拉馬丹

+0

你不關心整數第一關鍵? – powtac 2011-03-03 11:07:04

+0

對不起,我沒有得到哪一個是int第一個鍵,產品數組上的第一個鍵? – 2011-03-03 11:11:06

+0

不管,第一個數組中的0-1和第二個數組中的0-7。但無論如何檢查我的解決方案:) – powtac 2011-03-03 11:13:04

回答

2
// Use pid as key 
foreach ($arr1 as $key => $value) { 
    $arr1_new[$value['pid']] = $value; 
} 

// Move comments into $arr1_new 
foreach($arr2 as $key => $value) { 
    $arr2_new[$value['pid']]['comments'][] = $value['comment']; 
    // $arr2_new[$value['pid']]['comments'][] = $value; // if you really need all information here... 
} 

var_dump($arr2_new); 
+0

結果是什麼樣子?如何「混合」? – powtac 2011-03-03 11:17:16

+0

我的錯,在第二個foreach中將'$ arr1_new'更改爲'$ arr2_new',它現在應該可以工作。 – powtac 2011-03-03 11:21:12

+0

謝謝powtac,你爲我節省了很多時間,6個小時已經把這件事情弄清楚了。逐一測試php數組函數。可能上帝在某一天會在某種程度上回報你:D – 2011-03-03 11:49:56