php插入数组在特定位置的其他数组?

时间:2016-02-01 19:40:52

标签: php arrays

我想在特定区域的detail array中插入data array

例如,bdhdetail array键的值应放在bdh的{​​{1}}键中,替换那里的空数组首先。同样,data arrayhadoop键的值应替换detail array当前所拥有的hadoop键的空数组。对data array数组中的每个键都应该进行此替换。

我如何做到这一点?

数据阵列

details

详情数组

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

1 个答案:

答案 0 :(得分:0)

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