2017-07-14 3 views
-1

Ich möchte ein anderes Element in meine vorhandene JSON-Array einfügen.PHP - Element in verschachtelten JSON-Array hinzufügen

{ 
    "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 "Galerie 1" oder "Gallery 2" und so weiter ... Ein breiteres Bild hinzugefügt werden soll.

Wie kann ich dem entsprechenden "Titel" ein neues bildspezifisches hinzufügen?

+1

Der praktischste Ansatz könnte sein, Ihr JSON-String in ein PHP-Objekt zu konvertieren mit json_decode(). Dann können Sie das Objekt direkt manipulieren, darüber iterieren usw. Wenn ich ein gutes Beispiel finden kann, werde ich es hier veröffentlichen. –

Antwort

0

Hier ist, wie ich es tun würde. Beispiel hier: 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

Hier ist die Möglichkeit, weitere Elemente in Ihrem JSON hinzuzufügen. Konvertiere json string in array füge Element hinzu wo du willst und konvertiere wieder in 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)); 
    ?> 

Live-Demo: https://eval.in/832742

+0

WOW, so schnell! Genau das, wonach ich gesucht habe. Das "[0]" kann ich ja mit foreach finden (Galerie 1 = [0], Galerie 2 = [1] etc ...) Vielen Dank :) –

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

}