php中的数组按数组键合并?
是否有一个可以按键组合数组的好函数?(本例中为pid)php中的数组按数组键合并?,php,Php,是否有一个可以按键组合数组的好函数?(本例中为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
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
'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(嵌套在产品数组上)
谢谢你的光临
Adam Ramadhan你不在乎整数第一个键吗?对不起,我不知道哪一个是int第一个键,products数组中的第一个键?不要紧,第一个数组中的0-1和第二个数组中的0-7。但请检查我的解决方案:)结果是什么样的?多“混合”?我的错,在第二个foreach中将
$arr1_new
更改为$arr2_new
,现在应该可以工作了。谢谢你,powtac,你节省了我很多时间,这件事已经解决了6个小时了。逐个测试php数组函数。愿上帝有一天以某种方式报答你:DNo prob Adam,一旦每个人都必须学会如何处理多维数组;)
// 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);
// 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);