php将children数组转换为父数组

时间:2018-02-19 05:33:55

标签: php arrays multidimensional-array

我正在获取数据并试图将children数据推送到他的父数组中,但没有在数组中获得正确的索引。

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_push,之后我使用array_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);
}

2 个答案:

答案 0 :(得分:0)

您希望从this问题中获取所有子级别2数组。 并使用array_merge_recursive()合并数组。

$newarray = array_merge_recursive($oldarray,$newarray);

请参阅文档here

答案 1 :(得分:-1)

你可以试试这段代码

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);
}