PHP - 在嵌套的JSON-Array中添加项目

时间:2017-07-14 18:08:28

标签: php

我想在现有的JSON数组中插入另一个项目。

{
    "gallery": [
    {
                    "titel": "Gallery 1",
                    "thumb": "http://via.placeholder.com/150x150",
                    "images": [{
                                    "image": "http://via.placeholder.com/150x150"
                            },{
                                    "image": "http://via.placeholder.com/150x150"
                            },{
                                    "image": "http://via.placeholder.com/150x150"
                            },{
                                    "image": "http://via.placeholder.com/150x150"
                            },{
                                    "image": "http://via.placeholder.com/150x150"
                            },{
                                    "image": "http://via.placeholder.com/150x150"
                            }]
    }, {
                    "titel": "Gallery 2",
                    "thumb": "http://via.placeholder.com/150x150",
                    "images": [{
                                    "image": "http://via.placeholder.com/150x150"
                            },{
                                    "image": "http://via.placeholder.com/150x150"
                            }]
    }
]
}

In" Gallery 1"或"画廊2"等等... 将添加更广泛的图片。

如何将新的图像特定添加到相应的"标题"?

3 个答案:

答案 0 :(得分:0)

这是在json中添加更多元素的方法。将json字符串转换为数组添加元素,然后再将其转换为json。

    <?php
    $str = '{
        "gallery": [
        {
                        "titel": "Gallery 2",
                        "thumb": "http://via.placeholder.com/150x150",
                        "images": [{
                                        "image": "http://via.placeholder.com/150x150"
                                },{
                                        "image": "http://via.placeholder.com/150x150"
                                }]
        }
    ]}';
    $str_arr = json_decode($str,true);
    print_r($str_arr);
    $str_arr['gallery'][0]['images'][] = ["image"=>"http://new_url.com/150x150"];
    print_r(json_encode($str_arr));
    ?>

现场演示:https://eval.in/832742

答案 1 :(得分:0)

我将如何做到这一点。这里的例子: https://iconoun.com/demo/temp_mrdoe.php

<?php // demo/temp_mrdoe.php
/**
 * Dealing with JSON document
 *
 * https://stackoverflow.com/questions/45109267/php-add-item-in-nested-json-array
 */
error_reporting(E_ALL);
echo '<pre>';

// TEST DATA FROM THE POST AT STACK
$json = <<<EOD
{
    "gallery": [
    {
                    "titel": "Gallery 1",
                    "thumb": "http://via.placeholder.com/150x150",
                    "images": [{
                                    "image": "http://via.placeholder.com/150x150"
                            },{
                                    "image": "http://via.placeholder.com/150x150"
                            },{
                                    "image": "http://via.placeholder.com/150x150"
                            },{
                                    "image": "http://via.placeholder.com/150x150"
                            },{
                                    "image": "http://via.placeholder.com/150x150"
                            },{
                                    "image": "http://via.placeholder.com/150x150"
                            }]
    }, {
                    "titel": "Gallery 2",
                    "thumb": "http://via.placeholder.com/150x150",
                    "images": [{
                                    "image": "http://via.placeholder.com/150x150"
                            },{
                                    "image": "http://via.placeholder.com/150x150"
                            }]
    }
    ]
}
EOD;

// MAKE AN OBJECT FROM THE JSON STRING
$obj = json_decode($json);

// MAKE A NEW IMAGE OBJECT TO ADD TO "Gallery 2" IMAGES
$img = new StdClass;
$img->image = 'http://via.placeholder.com/THIS_IS_MY_NEW_IMAGE';

// FIND "Gallery 2" AND ADD THE NEW OBJECT
foreach ($obj->gallery as $key => $g)
{
    if ($g->titel == 'Gallery 2')
    {
        $g->images[] = $img;
        $obj->gallery[$key] = $g;
    }
}

// ACTIVATE THIS TO VISUALIZE THE MUTATED OBJECT
// var_dump($obj);

// BACK TO JSON
$new = json_encode($obj, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
echo PHP_EOL . $new;

答案 2 :(得分:0)

function galleryAddItem($title) {
     $str = file_get_contents('gallery_json.json');
     $json = json_decode($str, true);
     $a = 0;
     foreach ($json['gallery'] as $key) {
             if ($key['titel'] == $title) {
                     $json['gallery'][$a]['images'][]['image'] = "test";
             }
             $a++;
     }
     print_r(json_encode($json));
     file_put_contents('gallery_json.json', json_encode($json));

}