0

I am getting data and trying to push children data into his parent array but not getting proper index in the array.

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
        )

)

but I want to merge all child values back into the parent array like this:

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
        )

)

I have tried with array_push and after that, I have used array_multisort not working. Any idea about this.

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);
}
Mr.Happy
  • 2,639
  • 9
  • 40
  • 73

2 Answers2

0

You want to get all child level 2 arrays from this question. And use array_merge_recursive() to merge arrays.

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

See Documentationhere

-1

you can try this code

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