简体   繁体   English

PHP的插入数组在其他地方在特定的地方?

[英]php insert array in other array in specific place?

I want to insert the detail array in the data array under specific areas. 我想在data array特定区域下插入detail array

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. 例如,所述的值bdh中关键detail array应被放置在bdh中的关键data array ,在更换空数组,它是有开始。 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. 同样, detail array hadoop键的值应替换data array hadoop键当前具有的空数组。 This replacement should happen for every key in the details array. 此替换应发生在details数组中的每个键上。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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