根据主数组值创建一个子数组

时间:2018-02-20 06:17:23

标签: php arrays multidimensional-array

我有两个数组,第一个数组是主注释,第二个数组是子注释。

第一阵列

Array
(
    [0] => Array
        (
            [id] => 1
            [comment] => Nice Blog
            [parent_id] => 0
            [created] => 2018-02-20 00:00:00
            [user_title] => Shaishav Desai
            [image_path] => 067da3ff3b891981caa5b5d98c44052c.png
            [cnt] => 
        )

    [1] => Array
        (
            [id] => 2
            [comment] => Awesome blog
            [parent_id] => 0
            [created] => 2018-02-20 00:00:00
            [user_title] => Lead1 Assistant
            [image_path] => 099dbe4c58606a3abf867d821e62fdf9.png
            [cnt] => 2
        )
)

第二阵列

Array
(
    [0] => Array
        (
            [id] => 3
            [comment] => Yes, Really good
            [parent_id] => 2
            [created] => 2018-02-20 00:00:00
            [user_title] => Shaishav Desai
            [image_path] => 067da3ff3b891981caa5b5d98c44052c.png
        )

    [1] => Array
        (
            [id] => 4
            [comment] => Thank you
            [parent_id] => 2
            [created] => 2018-02-20 00:00:00
            [user_title] => Lead1 Assistant
            [image_path] => 099dbe4c58606a3abf867d821e62fdf9.png
        )

)

现在我们需要一个像这样的数组,基于“parent_id”和“id”的单个数组:

我想要这样的新数组:

Array
(
    [0] => Array
        (
            [id] => 1
            [comment] => Nice Blog
            [parent_id] => 0
            [created] => 2018-02-20 00:00:00
            [user_title] => Shaishav Desai
            [image_path] => 067da3ff3b891981caa5b5d98c44052c.png
            [cnt] => 
        )

    [1] => Array
        (
            [id] => 2
            [comment] => Awesome blog
            [parent_id] => 0
            [created] => 2018-02-20 00:00:00
            [user_title] => Lead1 Assistant
            [image_path] => 099dbe4c58606a3abf867d821e62fdf9.png
            [cnt] => 2
            [sub_comment] => Array
            (
                [0] => Array
                    (
                        [id] => 3
                        [comment] => Yes, Really good
                        [parent_id] => 2
                        [created] => 2018-02-20 00:00:00
                        [user_title] => Shaishav Desai
                        [image_path] => 067da3ff3b891981caa5b5d98c44052c.png
                    )

                [1] => Array
                    (
                        [id] => 4
                        [comment] => Thank you
                        [parent_id] => 2
                        [created] => 2018-02-20 00:00:00
                        [user_title] => Lead1 Assistant
                        [image_path] => 099dbe4c58606a3abf867d821e62fdf9.png
                    )

            )
        )

)

所以我很容易知道这个评论有基于新数组的子评论和显示。 请您告诉我是否有可能?

提前致谢。

1 个答案:

答案 0 :(得分:0)

//假设$ array1是firts数组;

// $ array2是第二个数组

foreach($array2 as $subary){
    $cnt=0;
    foreach($array1 as $key=> $mainary){
        if($subary['parent_id']==$mainary['id']){
            $array1[$key]['sub_comment']=$subary;
        }
    }

}
print_r($array1);