【问题标题】:php push children array into the parent arrayphp将子数组推入父数组
【发布时间】:2018-02-19 05:33:55
【问题描述】:

我正在获取数据并尝试将 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);
}

【问题讨论】:

标签: php arrays multidimensional-array


【解决方案1】:

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

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

参见文档here

【讨论】:

    【解决方案2】:

    你可以试试这个代码

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 2013-07-30
      • 2013-09-10
      • 2015-11-18
      • 1970-01-01
      • 2012-03-22
      • 1970-01-01
      相关资源
      最近更新 更多