I want to read this JSON using PHP:
{
["person":{
{"name":"Bill"},
{"age":"56"},
{"city":"Houston"}
}],
["person":{
{"name":"Jack"},
{"age":"45"},
{"city":"Dallas"}
}],
["person":{
{"name":"Henry"},
{"age":"33"},
{"city":"Atlanta"}
}]
}
Now I want to look for the person who's name is Jack and get the city he lives in. I know how to check for the name. But how can I get the corresponding city then?
I think this should make it:
$string = file_get_contents("persons.json");
$json = json_decode($string);
foreach ($json->person as $person) {
if($person->name == "Jack")
{
$city = $person->city;
}
}