How to sort a multidimensional JSON array in PHP? -
this question has answer here:
would please tell me how can sort kind of json array "name"
in ascending alphabetical order?
{ "response": { "game_count": 86, "games": [ "10": { "appid": 10, "name": "counter-strike", "playtime_forever": 7604, "img_icon_url": "6b0312cda02f5f777efa2f3318c307ff9acafbb5", "img_logo_url": "af890f848dd606ac2fd4415de3c3f5e7a66fcb9f", "has_community_visible_stats": true }, "80": { "appid": 80, "name": "counter-strike: condition zero", "playtime_forever": 62, "img_icon_url": "077b050ef3e89cd84e2c5a575d78d53b54058236", "img_logo_url": "acdb28ba1b4c2fcc526332c1b63fc0f7533f087f" }, . . . ] } }
your question doesn't make clear language code in. assumed php because of php tag on question.
my approach ($json
raw json):
// decode json array $arr = json_decode($json,true); // usort allows sorting custom attribute usort( $arr['response']['games'], function($a,$b){ return $a['name'] < $b['name'] ? -1 : 1;} ); // print , check order print_r($arr); // turn json $json = json_encode($arr);
docs: usort
Comments
Post a Comment