I have a json file in format
{
"list" : {
"1" : {
"thing1" : "description",
"thing2" : "description",
"thing3" : "description"
},
"2" : {
"thing1" : "description",
"thing2" : "description",
"thing3" : "description"
},
etc.
}
I need to search through and return data based on the description of thing 2 but I also need to then return the number of the list. Problem is the json file comes in with the numbers all out of order so I can’t just increment a variable as I go through them all.
currently I have my code setup like this:
$json = json_decode($response);
foreach($json->list as $item) {
$i++;
if($item->thing2 == "description") {
echo "<p>$item->thing1</p>";
echo "<p>$item->thing2</p>";
echo "<p>$item->thing3</p>";
echo "<p>position: $i</p><br /><br />";
}
}
unfortunately because the positions are out of order everytime the $i variable is retuning the wrong position. How can I return the title of the item that has the proper thing2 description.
The setting the second parameter of
json_decode()toTRUEreturns an associative array which is a bit more conducive to what you want to do:Should do the trick.