0

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

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/

CodeBrauer
  • 2,690
  • 1
  • 26
  • 51

2 Answers2

1

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.

George Brighton
  • 5,131
  • 9
  • 27
  • 36
  • I have no idea why it was printed as object. This array is generated from `json_decode($content,true)` - i can handle it as an array. This solution didn't worked for me – CodeBrauer Oct 06 '13 at 14:54
  • It is sorted - but not alphabetical. Warning: strcmp() expects parameter 1 to be string, array given in C:\wamp\www\GIT\prj1\index.php on line 19 – CodeBrauer Oct 06 '13 at 15:05
  • thanks but also the array isn't sorted. (it's sorted like in the file) – CodeBrauer Oct 06 '13 at 15:11
1

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']);
}
HuyPV
  • 51
  • 1
  • 3