Reputation: 22810
is there a good function that can combine array by key ? ( in this example is 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)
and
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)
to something like
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)
or with no pid on each product comments array ( nested on products array ) ?
thanks for looking in.
Adam Ramadhan
Upvotes: 0
Views: 393
Reputation: 41050
// 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);
Upvotes: 2