I have a json file as below name "brands.json"
{
"title":"List of brands",
"version" : 1,
"nike": [
{"type":"shoes","size":10,"color":"black"},
{"type":"shirt","size":"S","color":"black"}
],
"converse": [
{"type":"shoes","size":10,"color":"red"},
{"type":"backpack","size":"N/A","color":"red"}
],
"champion": [
{"type":"shoes","size":10,"color":"blue"},
{"type":"pants","size":"M","color":"grey"}
]
}
I looked at some example online and get this piece of code
<?php
$read = file_get_contents("report.json");
$json = json_decode($read, true);
foreach($json as $key => $val){
if(is_array($val)){
echo "$key : <br>";
foreach($key as $k => $v){
echo "$k => $v<br>";
}
}
else {
echo "$key => $val<br>";
}
}
?>
I would be able to print out
title => List of brands
version => 1
nike :
converse :
champion :
But I would like to get the array inside of those brands. I was thinking of having a foreach
loop inside the if statement. However, it returns errors
nike : Warning: Invalid argument supplied for foreach().
Some resources suggested to do something like $json->nike as $item => $v
but that will be redundant since I also have converse and champion arrays. If someone could direct me to a good resource or provide a code example, it'd be very much appreciated.
Expected table Nike:
type | size | color
shoes| 10 | black
shirt| S | black