php将子数组推送到父数组中
我正在获取数据,并试图将php将子数组推送到父数组中,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我正在获取数据,并试图将子项数据推送到其父数组中,但没有在数组中获得正确的索引 Array ( [0] => stdClass Object ( [id] => 708 [user_id] => 104 [component] => activity [type] => activity_comment [action] =
子项
数据推送到其父数组中,但没有在数组中获得正确的索引
Array
(
[0] => stdClass Object
(
[id] => 708
[user_id] => 104
[component] => activity
[type] => activity_comment
[action] => XXXX XXXX posted a new activity comment
[content] => hello manan
[primary_link] =>
[item_id] => 707
[secondary_item_id] => 707
[date_recorded] => 2018-02-19 05:01:16
[hide_sitewide] => 0
[mptt_left] => 2
[mptt_right] => 5
[is_spam] => 0
[user_email] => xxxxxxx@xxxxx.xxx
[user_nicename] => manan88
[user_login] => manan88
[display_name] => XXXX XXXX
[user_fullname] => XXXX XXXX
[children] => Array
(
[710] => stdClass Object
(
[id] => 710
[user_id] => 104
[component] => activity
[type] => activity_comment
[action] => XXXX XXXX posted a new activity comment
[content] => hey
[primary_link] =>
[item_id] => 707
[secondary_item_id] => 708
[date_recorded] => 2018-02-19 05:02:10
[hide_sitewide] => 0
[mptt_left] => 3
[mptt_right] => 4
[is_spam] => 0
[user_email] => xxxxxxx@xxxxx.xxx
[user_nicename] => manan88
[user_login] => manan88
[display_name] => XXXX XXXX
[user_fullname] => XXXX XXXX
[children] => Array
(
)
[depth] => 2
)
)
[depth] => 1
)
[1] => stdClass Object
(
[id] => 709
[user_id] => 104
[component] => activity
[type] => activity_comment
[action] => XXXX XXXX posted a new activity comment
[content] => hello manan 2
[primary_link] =>
[item_id] => 707
[secondary_item_id] => 707
[date_recorded] => 2018-02-19 05:01:38
[hide_sitewide] => 0
[mptt_left] => 6
[mptt_right] => 7
[is_spam] => 0
[user_email] => xxxxxxx@xxxxx.xxx
[user_nicename] => manan88
[user_login] => manan88
[display_name] => XXXX XXXX
[user_fullname] => XXXX XXXX
[children] => Array
(
)
[depth] => 1
)
)
但我想将所有子值合并回父数组,如下所示:
Array
(
[0] => stdClass Object
(
[id] => 708
[user_id] => 104
[component] => activity
[type] => activity_comment
[action] => XXXX XXXX posted a new activity comment
[content] => hello manan
[primary_link] =>
[item_id] => 707
[secondary_item_id] => 707
[date_recorded] => 2018-02-19 05:01:16
[hide_sitewide] => 0
[mptt_left] => 2
[mptt_right] => 5
[is_spam] => 0
[user_email] => xxxxxxx@xxxxx.xxx
[user_nicename] => manan88
[user_login] => manan88
[display_name] => XXXX XXXX
[user_fullname] => XXXX XXXX
[children] => Array
(
[710] => stdClass Object
(
[id] => 710
[user_id] => 104
[component] => activity
[type] => activity_comment
[action] => XXXX XXXX posted a new activity comment
[content] => hey
[primary_link] =>
[item_id] => 707
[secondary_item_id] => 708
[date_recorded] => 2018-02-19 05:02:10
[hide_sitewide] => 0
[mptt_left] => 3
[mptt_right] => 4
[is_spam] => 0
[user_email] => xxxxxxx@xxxxx.xxx
[user_nicename] => manan88
[user_login] => manan88
[display_name] => XXXX XXXX
[user_fullname] => XXXX XXXX
[children] => Array
(
)
[depth] => 2
)
)
[depth] => 1
)
[1] => stdClass Object
(
[id] => 710
[user_id] => 104
[component] => activity
[type] => activity_comment
[action] => XXXX XXXX posted a new activity comment
[content] => hey
[primary_link] =>
[item_id] => 707
[secondary_item_id] => 708
[date_recorded] => 2018-02-19 05:02:10
[hide_sitewide] => 0
[mptt_left] => 3
[mptt_right] => 4
[is_spam] => 0
[user_email] => xxxxxxx@xxxxx.xxx
[user_nicename] => manan88
[user_login] => manan88
[display_name] => XXXX XXXX
[user_fullname] => XXXX XXXX
[children] => Array
(
)
[depth] => 2
)
[2] => stdClass Object
(
[id] => 709
[user_id] => 104
[component] => activity
[type] => activity_comment
[action] => XXXX XXXX posted a new activity comment
[content] => hello manan 2
[primary_link] =>
[item_id] => 707
[secondary_item_id] => 707
[date_recorded] => 2018-02-19 05:01:38
[hide_sitewide] => 0
[mptt_left] => 6
[mptt_right] => 7
[is_spam] => 0
[user_email] => xxxxxxx@xxxxx.xxx
[user_nicename] => manan88
[user_login] => manan88
[display_name] => XXXX XXXX
[user_fullname] => XXXX XXXX
[children] => Array
(
)
[depth] => 1
)
)
我尝试过使用array\u push
,之后,我使用的array\u multisort
不起作用。你知道吗
for ($i=0; $i<count($data); $i++) {
// Move children to main array
$children = $data[$i]->children;
if (!empty($children)) {
foreach ($children as $key => $value) {
array_push($data, $children[$key]);
}
}
}
if (!empty($data)) {
array_multisort($data, SORT_ASC);
}
$i=0;$ichildren;
如果(!空($children)){
foreach($key=>$value的子项){
数组_push($data,$children[$key]);
}
}
}
如果(!空($data)){
数组\多排序($data,排序\ ASC);
}
您希望从问题中获取所有子级别2数组。 并使用
array\u merge\u recursive()
合并数组
$newarray = array_merge_recursive($oldarray,$newarray);
请参阅文档您可以尝试此代码
for ($i=0; $i<count($data); $i++) {
// Move children to main array
$children = $data[$i]->children;
if (!empty($children)) {
array_push($data, $children);
}
}
if (!empty($data)) {
array_multisort($data, SORT_ASC);
}
$i=0;$ichildren;
如果(!空($children)){
数组推送($data,$children);
}
}
如果(!空($data)){
数组\多排序($data,排序\ ASC);
}