2017-07-14 89 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)); 

}