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.
PS : I have already tried solutions that are mentioned in other SO answers, but they seems to be not working with my case.
Here we are using usort
for sorting.
usort($array["Places_nearby"],function($value1,$value2){
return $value1["place_rating"]>$value2["place_rating"];
});
print_r($array);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.