如何安排父子数组值?

时间:2018-01-22 10:31:20

标签: php arrays

我想安排从子到父的数组值,例如假设我有一个父数组A和B,A& B也有子数组,如X(A的子)和Y,Z(B的子),所以我希望X在父数组A和Y下的所有细节,Z所有细节父数组B.

以下是我的数组

{
    "status": 200,
    "message": "Success",
    "child": {
        "Parent": [
            {
                "df_cat_id": "3",
                "df_cat_name": "Food",
                "df_cat_image": "44ce23c62a6606de37dfb1a951766ad3.png",
                "create_date": "2018-01-18 09:13:48",
                "update_date": "2018-01-18 09:13:48"
            },
            {
                "df_cat_id": "5",
                "df_cat_name": "Beauty & Spa",
                "df_cat_image": "875bee6f51fd88f2cfb40ff373383c26.png",
                "create_date": "2018-01-22 09:36:17",
                "update_date": "2018-01-22 09:36:17"
            }
        ],
        "childData": [
            [
                {
                    "df_scat_id": "1",
                    "parent_cat_id": "3",
                    "df_scat_name": "Restaurants",
                    "fd_scat_image": "fe49a6b5deade12e0b5a3b5845dbecb1.png",
                    "create_date": "2018-01-18 09:57:17",
                    "update_date": "2018-01-18 09:57:17"
                },
                {
                    "df_scat_id": "4",
                    "parent_cat_id": "3",
                    "df_scat_name": "Cafe",
                    "fd_scat_image": "b1333836b2384c3b3c1eebbb1e7daed0.png",
                    "create_date": "2018-01-18 11:21:09",
                    "update_date": "2018-01-18 11:21:09"
                }
            ],
            [
                {
                    "df_scat_id": "5",
                    "parent_cat_id": "5",
                    "df_scat_name": "Beauty Parlor",
                    "fd_scat_image": "b2a73830a9d56fcc9b963ec66c648d8a.png",
                    "create_date": "2018-01-22 09:37:32",
                    "update_date": "2018-01-22 09:37:32"
                }
            ]
        ]
    }
}

我想按照以下方式输出..

{
    "status": 200,
    "message": "Success",
    "child": {
        "Parent": [
            {
                "df_cat_id": "3",
                "df_cat_name": "Food",
                "df_cat_image": "44ce23c62a6606de37dfb1a951766ad3.png",
                "create_date": "2018-01-18 09:13:48",
                "update_date": "2018-01-18 09:13:48",
                "childData":[
                        {
                        "df_scat_id": "1",
                        "parent_cat_id": "3",
                        "df_scat_name": "Restaurants",
                        "fd_scat_image": "fe49a6b5deade12e0b5a3b5845dbecb1.png",
                        "create_date": "2018-01-18 09:57:17",
                        "update_date": "2018-01-18 09:57:17"
                         },
                        {
                            "df_scat_id": "4",
                            "parent_cat_id": "3",
                            "df_scat_name": "Cafe",
                            "fd_scat_image": "b1333836b2384c3b3c1eebbb1e7daed0.png",
                            "create_date": "2018-01-18 11:21:09",
                            "update_date": "2018-01-18 11:21:09"
                        }
                ]
            },
            {
                "df_cat_id": "5",
                "df_cat_name": "Beauty & Spa",
                "df_cat_image": "875bee6f51fd88f2cfb40ff373383c26.png",
                "create_date": "2018-01-22 09:36:17",
                "update_date": "2018-01-22 09:36:17",
                "childData":[
                     {
                        "df_scat_id": "5",
                        "parent_cat_id": "5",
                        "df_scat_name": "Beauty Parlor",
                        "fd_scat_image": "b2a73830a9d56fcc9b963ec66c648d8a.png",
                        "create_date": "2018-01-22 09:37:32",
                        "update_date": "2018-01-22 09:37:32"
                     }
            }
        ]       
    }
}

...代码

<?php
$sql = "SELECT * FROM dynamic_parent_category";
        $query=$this->db->query($sql);
        $response =$query->result_array();

        foreach ($response as $key => $value) {
            $parentCatId = $value['df_cat_id'];






            $getParent = "SELECT * FROM dynamic_parent_category WHERE df_cat_id =$parentCatId";
            $parentQuery=$this->db->query($getParent);
            $parenDetails =$query->result_array();


            $getChildCate = "SELECT * FROM dynamic_sub_category WHERE parent_cat_id =$parentCatId";
            $childCate=$this->db->query($getChildCate);
            $childData =$childCate->result_array();
            $data['parnet'] =$parentCatId;

            $data['child'] =$childData;



            $output[] =$data;

        }

        //echo "prr".$parentCatId;


        return $output;
?>

0 个答案:

没有答案