简体   繁体   中英

php insert array in other array in specific place?

I want to insert the detail array in the data array under specific areas.

For instance, the value of the bdh key in the detail array should be placed in the bdh key of the data array , replacing the empty array that is there to begin with. Similarly, the value of the hadoop key in the detail array should replace the empty array that the hadoop key in the data array currently has. This replacement should happen for every key in the details array.

How do I accomplish this?

Data array

{"data":
    {
        "categories":
        {
            "articles":
            {
                "bdh":[],
                "hadoop":[]
            },
            "videos":
            {
                "bdh Videos":[],
                "hadoop Videos":[]
            }   
        }
    }
}

Detail array

{"details":{
    "bdh":
        [
            {"id":1, "name":"bdh article 1", "body":"this is bdh article 1 body."},
            {"id":2, "name":"bdh article 2", "body":"this is bdh article 2 body."}
        ],
    "hadoop":
        [
            {"id":3, "name":"hadoop article 1", "body":"this is hadoop article 1 body."},
            {"id":4, "name":"hadoop article 2", "body":"this is hadoop article 2 body."}
        ],


    "bdh Videos":
        [
            {"id":5, "name":"bdh videos 1", "body":"this is bdh videos 1 body."},
            {"id":6, "name":"bdh videos 2", "body":"this is bdh videos 2 body."}
        ],
    "hadoop Videos":
        [
            {"id":7, "name":"hadoop videos 1", "body":"this is hadoop videos 1 body."},
            {"id":8, "name":"hadoop videos 2", "body":"this is hadoop videos 2 body."}
        ]
}
<?php

    $videos = '
        {"data":
            {
                "categories":
                {
                    "articles":
                    {
                        "bdh":[],
                        "hadoop":[]
                    },
                    "videos":
                    {
                        "bdhVideos":[],
                        "hadoopVideos":[]
                    }   
                }
            }
        }
    ';

    $details='
        {"details":{
            "bdh":
                [
                    {"id":1, "name":"bdh article 1", "body":"this is bdh article 1 body."},
                    {"id":2, "name":"bdh article 2", "body":"this is bdh article 2 body."}
                ],
            "hadoop":
                [
                    {"id":3, "name":"hadoop article 1", "body":"this is hadoop article 1 body."},
                    {"id":4, "name":"hadoop article 2", "body":"this is hadoop article 2 body."}
                ],


            "bdhVideos":
                [
                    {"id":5, "name":"bdh videos 1", "body":"this is bdh videos 1 body."},
                    {"id":6, "name":"bdh videos 2", "body":"this is bdh videos 2 body."}
                ],
            "hadoopVideos":
                [
                    {"id":7, "name":"hadoop videos 1", "body":"this is hadoop videos 1 body."},
                    {"id":8, "name":"hadoop videos 2", "body":"this is hadoop videos 2 body."}
                ]
            }
        }
    ';

    $arr_videos = json_decode($videos);
    $arr_details = json_decode($details);

    //bdh
    $arr_videos->data->categories->articles->bdh = $arr_details->details->bdh[0];
    //hadoop
    $arr_videos->data->categories->articles->hadoop = $arr_details->details->hadoop[0];

    //videos bdh
    $arr_videos->data->categories->videos->bdh = $arr_details->details->bdhVideos;

    //videos hadoop
    $arr_videos->data->categories->videos->hadoop = $arr_details->details->hadoopVideos;

    print_r($arr_videos);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM