I would like to know how to search for data within a JSON using PHP. I have a JSON code that contains a huge list of cities with their id codes and I want the user to ask for a city and the program written in PHP looks for the city within the json, sees the similar cities, returns those matches to the user, he chooses and once we know the exact city, we look for it in the json and get the code from it.
So, how do I do that PHP lookup of the city in a JSON? The JSON has these characteristics:
[
{
"id": 707860,
"name": "Hurzuf",
"country": "UA",
"coord": {
"lon": 34.283333,
"lat": 44.549999
}
},
{
"id": 519188,
"name": "Novinki",
"country": "RU",
"coord": {
"lon": 37.666668,
"lat": 55.683334
}
}
]
Thank you very much for the help.
Assuming you already have the JSON in an associative array, you could iterate through it with a
foreach
and then store the results you want in another associative array. You can then manipulate the results as you wish. Something like that: