简体   繁体   中英

Sort an Array in PHP by a specific value

How can i sort this array by $data['response']['games'][x]['name'] alphabetical from AZ?

I have tried already array_multisort() but didn't understand this function at all.

Hope you can help me - googled and searched this but didn't found any solution for my problem.

Edit: link updated.

Code: https://github.com/GabrielWanzek/GWSteamLib/

You can achieve this with usort() , which allows you to define a custom comparison function:

usort($data['response']['games'], function($a, $b) {
    return strcmp($a['name'], $b['name']);
});

Note that $data is an object of type stdClass ; it is not an array.

Try following code:

$games = $data['response']['games']; // is array
usort($games, 'compareName');
var_dump($games);

# want to change $data?
# $data['response']['games'] = $games;

function compareName($a1, $a2) {
    return strcmp($a1['name'], $a2['name']);
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM