对不起,已经让标题变得复杂。
我希望将一个数组合并到一个已经创建的大规模多维数组中。
第一个想法......是否可以将子数组创建为数组();在创建时将数组循环到?
e.g
foreach ($records as $record) {
$record['AddedContent']['Comments'] = array(); //doesn't currently exist
foreach ($comments as $comment) {
if($record['AddedContent']['id'] = $comment['content_id']){
array_push($record['AddedContent']['Comments'], $comment);
}
}
}
这里评论是我要加入记录的小数组(大数组)
数组如下:
[0] => Array
(
[SubscribedToProfile] => Array
(
[id] =>
[user_id] =>
[company_name] =>
[picture_filename] =>
)
[AddedContent] => Array
(
[text] =>
[file_location] =>
[content_type_id] => 4
[file_name] => IxnAvLKMtIU
[id] => 87
)
)
评论数组是:
Array
(
[0] => Array
(
[ContentComment] => Array
(
[id] => 3
[content_id] => 87
[text] => jhbuoijniknb
[created] => 2013-10-21 13:08:12
[user_id] => 2097
)
)
[1] => Array
(
[ContentComment] => Array
(
[id] => 4
[content_id] => 88
[text] => gfrgfrtegvrtegvrtgvrtvtrgv
[created] => 2013-10-21 13:08:12
[user_id] => 2097
)
)
)
我需要它:
[0] => Array
(
[SubscribedToProfile] => Array
(
[id] =>
[user_id] =>
[company_name] =>
[picture_filename] =>
)
[AddedContent] => Array
(
[text] =>
[file_location] =>
[content_type_id] => 4
[file_name] => IxnAvLKMtIU
[id] => 87
[comments] => Array
(
[0] => Array
(
[id] => 2
[content_id] => 87
[text] => fderwcfrvfcerwfvrev
[created] => 2013-10-21 13:05:58
[user_id] => 2097
)
[1] => Array
(
[id] => 3
[content_id] => 87
[text] => jhbuoijniknb
[created] => 2013-10-21 13:08:12
[user_id] => 2097
)
)
)
)
我需要遍历bpth数组,因为我需要有条件地加入htem,看看ID是87(在这个例子中),内容ID是87。
答案 0 :(得分:0)
您的解决方案就在这里。
foreach ($records as $key=>$record) {
$array = array();
if($key=='AddedContent') {
foreach($comments as $comment) {
if($record['id'] == $comment["ContentComment"]['content_id']){
array_push($array, $comment["ContentComment"]);
}
}
$records[$key]['comments'] = $array;
}
}