使用PHP,我如何将JSON blob附加到现有的多级JSON对象


Using PHP, how do I append a JSON blob to an existing multi-level JSON object

我正试图从外部文件中获取一些JSON数据,并将一些用户生成的JSON附加到其中,这些数据通过GET发送。在本例中,我删除了GET业务,并提供了新的JSON作为变量。

旧数据来自data/test.json,并存储在变量$oldData中。

新数据将来自GET,但在本测试中,它位于变量$newData


这是来自data/test.json 的JSON

{
    "monkeys": [
        {
            "id": 1424634259848,
            "name": "Funky Monkey",
            "description": "This sure is a funky monkey!",
            "favoriteFoods": [
                "bananas",
                "cheese"
            ],
            "image": "img/funky-monkey.jpg"
        }
    ]
}

以下是让我走到这一步的PHP

$fileData = file_get_contents('data/test.json');
$oldData = json_decode($fileData, true);
$newData = json_decode('{"id": 1424634259848,"name": "Monkey Ball","description": "This is a video game, not a monkey!","favoriteFoods": ["bits","pixels"],"image": "img/monkey-ball.jpg"}', true);
$result = array_merge_recursive($oldData,$newData);
echo '<pre>' . print_r(json_encode($result), true) . '</pre>';

这是的结果

{
    "monkeys": [
        {
            "id": 1424634259848,
            "name": "Funky Monkey",
            "description": "This sure is a funky monkey!",
            "favoriteFoods": [
                "bananas",
                "cheese"
            ],
            "image": "img/funky-monkey.jpg"
        }
    ],
    "id": 1424634259848,
    "name": "Monkey Ball",
    "description": "This is a video game, not a monkey!",
    "favoriteFoods": [
        "bits",
        "pixels"
    ],
    "image": "img/monkey-ball.jpg"
}

不幸的是,第二只猴子在括号外,而不是猴子阵列中的第二个项目。

这是我想要的结果

{
    "monkeys": [
        {
            "id": 1424634259848,
            "name": "Funky Monkey",
            "description": "This sure is a funky monkey!",
            "favoriteFoods": [
                "bananas",
                "cheese"
            ],
            "image": "img/funky-monkey.jpg"
        },
        {
            "id": 1424634259848,
            "name": "Monkey Ball",
            "description": "This is a video game, not a monkey!",
            "favoriteFoods": [
                "bits",
                "pixels"
            ],
            "image": "img/monkey-ball.jpg"
        }
    ]
}

我不关心JSON的键或值,我只想将这个新的JSON blob附加为monkey下的下一个索引。

那么,长话短说,使用PHP,我如何将blob附加到多级JSON对象的末尾?

此外,我很感激在栈溢出上有很多类似的问题,也许我错过了一个涉及这个主题的问题。如果你知道一个涉及这方面的问题,请通过链接发表评论。

以下是您想要的:

<?php
     $json = '{
    "monkeys": [
        {
            "id": 1424634259848,
            "name": "Funky Monkey",
            "description": "This sure is a funky monkey!",
            "favoriteFoods": [
                "bananas",
                "cheese"
            ],
            "image": "img/funky-monkey.jpg"
        }
    ]
}';
    $jsonArray = json_decode($json,true);
    $newJson = '{"id": 1424634259848,"name": "Monkey Ball","description": "This is a video game, not a monkey!","favoriteFoods": ["bits","pixels"],"image": "img/monkey-ball.jpg"}';
    $newJson = json_decode($newJson,true);
    $jsonArray['monkeys'][] = $newJson;
    $ultimateJson = json_encode($jsonArray);
    var_dump($ultimateJson);

您正在组合两个数组,而不是将一个数组附加到另一个数组。http://codepad.viper-7.com/Uy3Zbn

您想要将新数组推送到旧数组的monkeys元素上:

$oldData['monkeys'][] = $newData;