douzhu1188 2016-02-01 19:40
浏览 69

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

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."}
        ]
}
  • 写回答

1条回答 默认 最新

  • duanhao1004 2016-02-01 20:12
    关注
    <?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);
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条