Apologies if this has been asked a thousand times, but I can't find a good tutorial on how to do this correctly and searching on Stack is coming up trumps.
I have a JSON file which has data like this:
{
"store":"Store 1",
"cat":"Categories",
"general_cat":"Categories",
"spec_cat":"Accessories"
},
{
"store":"Store 1",
"cat":"Categories",
"general_cat":"Categories",
"spec_cat":"Accessories"
},
with about 50 entries in it. I'm trying to parse this data and store the values in variables.
So far I've tried:
$string = file_get_contents("jsonFile.json");
$json_array = json_decode($string,true);
foreach ($json_array as $key => $value){
$store = $key -> store;
$general_cat = $key -> general_cat;
$spec_cat = $key -> spec_cat;
if (!is_null($key -> mainImg_select)){
$cat = $key -> cat;
}
echo $headURL;
}
This is resulting in "Trying to get property of non object" errors. What am I doing wrong here?
[and]like[{...},{...}]. Secondly, you are using the valuetruefor the second parameter ofjson_decode. If it is set to true, you will not manipulate object, but associative arrays. So,$key->storewill not be available. At the same time it may be instead$value["store"].$valueand not$key. Please read completely my answer. Afterjson_decode,$json_arrayis an array of associative arrays. So,$keyis a numeric index, and$valuethe array you wat to manipulate.