I have a JSON file, I am trying to write a script to loop through the json file. I am stuck in the part where value contains multiple images.
[
{
"id" : "1",
"name" : "Michel",
"value" : "Michel is from US"
},
{
"id" : "2",
"value" : "Marko is from German",
"name" : "Marko"
},
{
"id" : "3",
"name" : "Adam",
"value" : "Adam is from France"
},
{
"id" : "4",
"name" : "Photos",
"value" : [
{
"image" : "images/michel.jpg"
},
{
"image" : "images/marko.jpg"
},
{
"image" : "images/adam.jpg"
}
]
},
{
"id" : "5",
"name" : "MARKO",
}
{
"id" : "6",
"name" : "EDDI",
"value" : [
"001",
"002"
],
}
Json file looks like written above,
$json = file_get_contents("json/example.json");
// Convert JSON string to Object
$object = json_decode($json);
foreach($object as $key => $value) {
echo $value->name. ", " . $value->value . "<br>";
}
I got what I was supposed except for values that contain more images, for that part it displays errors.
foreach($object as $key => $value) {
echo $value->name. ", " . $value->value . "<br>";
}
ERRORS:
Notice: Array to string conversion in C:\xampp\htdocs\test\index.php on line 19
Notice: Undefined property: stdClass::$value in C:\xampp\htdocs\test\index.php on line 19
If a value doesn't exist, I need to print only the "name" andvalue will be empty.
Any advice is appreciated