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 valuetrue
for the second parameter ofjson_decode
. If it is set to true, you will not manipulate object, but associative arrays. So,$key->store
will not be available. At the same time it may be instead$value["store"]
.$value
and not$key
. Please read completely my answer. Afterjson_decode
,$json_array
is an array of associative arrays. So,$key
is a numeric index, and$value
the array you wat to manipulate.