2017-07-14 55 views
-1

我想插入另一个项目到我现有的JSON数组中。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" 
          }] 
    } 
] 
} 

在“相册1”或“图库2”等等... 较宽的图象是要被添加。

如何为相应的“标题”添加新的特定图像?

+1

最实用的方法可能是您的JSON字符串转换成一个PHP对象与JSON_Decode()。然后你可以直接操纵对象,迭代它等等。如果我能找到一个好例子,我会在这里发布它。 –

回答

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;

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

+0

哇,太快了!正是我在找的东西。我确实可以用foreach找到[[0]](Gallery 1 = [0],Gallery 2 = [1]等等......) 非常感谢:) –

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

}