I have the following JSON array in my PHP
{
"responseHeader": {
"type": "Places",
"status": "200",
"message": "Places fetched"
},
"Places_nearby": [{
"place_name": "blueblue",
"place_rating": "5"
},
{
"place_name": "qwer",
"place_rating": "10"
},
{
"place_name": "mvb",
"place_rating": "0.6"
},
{
"place_name": "tyu",
"place_rating": "25"
},
{
"place_name": "erty",
"place_rating": "1"
},
{
"place_name": "Malabar Adukkala",
"place_rating": "7"
}
],
"Google_places_most_rated": [{
"place_name": "Malabar Adukkala",
"place_rating": "5"
},
{
"place_name": "Malabar Adukkala",
"place_rating": "5"
}
]
}
I need to sort the "Places_nearby" array inside it by increasing value of "place_rating".
What I did was decoding the JSON in my code,
json_decode($json,true)
And then using usort as follows
function sortfunction($a, $b) {
return strcasecmp($a['place_rating'], $b['place_rating']);
}
usort($data['Places_nearby'], 'sortfunction');
But nothing happens.
How can I rewrite usort function, or do I want to write my custom function to sort the array? I thought that if PHP had some internal sporting mechanisms, that would be faster than other methods I can write.
P.S : I have already tried solutions that are mentioned in other SO answers, but they seems to be not working with my case.
place_ratingbeforeplace_namein each subarray, then you can simply use:sort($array["Places_nearby"]);because it will sort on the first element in the subarrays. Demo