0

I have a two-dimensional array and want to sort it by name. I would like to use usort() with anonymous functions. How should my comparator function look like when I want to sort something alphabetically?

[names] => Array
(
    [0] => Array
        (
            [name] => Baba
            [prename] => Ali
        )

    [1] => Array
        (
            [name] => Pan
            [prename] => Peter
        )

)

To sort, I tried this:

usort($names, function cmp($a, $b) {
    return strcmp($a['name'], $b['name']);
});

which gives me unexpected T_STRING, expecting '(' on the first line.

hakre
  • 193,403
  • 52
  • 435
  • 836
a1337q
  • 770
  • 3
  • 10
  • 20

2 Answers2

1

You can use usort

$names = Array(
"0" => Array("name" => "B","prename" => "A"),
"1" => Array("name" => "D","prename" => "B"),
"2" => Array("name" => "A","prename" => "C"),
"3" => Array("name" => "B","prename" => "D")

        );

Example 1

__xsort($names,"name");
var_dump($names);

Output

array
  0 => 
    array
      'name' => string 'A' (length=1)
      'prename' => string 'C' (length=1)
  1 => 
    array
      'name' => string 'B' (length=1)
      'prename' => string 'A' (length=1)
  2 => 
    array
      'name' => string 'B' (length=1)
      'prename' => string 'D' (length=1)
  3 => 
    array
      'name' => string 'D' (length=1)
      'prename' => string 'B' (length=1)

Example 2

__xsort($names,"prename");
var_dump($names);

Output

array
  0 => 
    array
      'name' => string 'B' (length=1)
      'prename' => string 'A' (length=1)
  1 => 
    array
      'name' => string 'D' (length=1)
      'prename' => string 'B' (length=1)
  2 => 
    array
      'name' => string 'A' (length=1)
      'prename' => string 'C' (length=1)
  3 => 
    array
      'name' => string 'B' (length=1)
      'prename' => string 'D' (length=1))

Function Used

function __xsort(&$names,$key) {
    usort($names, function ($a, $b) use($key) {
        if ($a[$key] == $b[$key]) {
            return 0;
        }
        return ($a[$key] < $b[$key]) ? - 1 : 1;
    });

    return $names;
}
Baba
  • 94,024
  • 28
  • 166
  • 217
  • 1
    +1 for this general array sorting function, thanks. – a1337q Oct 06 '12 at 12:18
  • php has [`array_multisort`](http://php.net/array_multisort) for that. – hakre Oct 06 '12 at 15:45
  • @hakre you can not sort with keys in `array_multisort` have also been looking for you can you look at http://stackoverflow.com/questions/12759011/can-some-break-down-this-php-code for me ... I think you understand PHP internals a lot better – Baba Oct 06 '12 at 15:48
  • @Baba: sure you can sort with keys, however you're more missing the [`array_column` function](http://stackoverflow.com/a/11558780/367456) I feel. See as well: https://wiki.php.net/rfc/array_column – hakre Oct 06 '12 at 16:05
  • @hakre yeah something similar but rather than `count` mine is `sorting ` – Baba Oct 06 '12 at 16:10
  • 1
    @Baba: you get the array of the column you want to sort, then you use `multisort`. done. This is how array_column works with sorting a multidimenstional array. – hakre Oct 06 '12 at 16:30
  • +1 Nice i get the angle you are coming from ..... nice one – Baba Oct 06 '12 at 16:31
1

The bug was not so obvious, but thinking again about the concept of anonymous functions, I got the hint. The function name can't be there. The correct solution for my exact problem is

usort($names, function ($a, $b) {
    return strcmp($a['name'], $b['name']);
});
a1337q
  • 770
  • 3
  • 10
  • 20