-1

I have an array called "$myarray", which looks like this:

[0] => Array
    (
        [PrinterID] => 4
        [PrinterName] => PRT04_GL
        [UserID] => 1
        [isDefaultPrinter] => 0
        [isMapped] => 0
    )

[1] => Array
    (
        [PrinterID] => 1
        [PrinterName] => PRT01_Zentral
        [UserID] => 
        [isDefaultPrinter] => 0
        [isMapped] => 0
    )

[2] => Array
    (
        [PrinterID] => 2
        [PrinterName] => PRT02_BH
        [UserID] => 
        [isDefaultPrinter] => 0
        [isMapped] => 0
    )

[3] => Array
    (
        [PrinterID] => 3
        [PrinterName] => PRT03_EDV
        [UserID] => 
        [isDefaultPrinter] => 0
        [isMapped] => 1
    )

I want to sort the data by ["PrinterName"]. The wanted result would be:

[0] => Array
    (
        [PrinterID] => 1
        [PrinterName] => PRT01_Zentral
        [UserID] => 
        [isDefaultPrinter] => 0
        [isMapped] => 0
    )

[1] => Array
    (
        [PrinterID] => 2
        [PrinterName] => PRT02_BH
        [UserID] => 
        [isDefaultPrinter] => 0
        [isMapped] => 0
    )

[2] => Array
    (
        [PrinterID] => 3
        [PrinterName] => PRT03_EDV
        [UserID] => 
        [isDefaultPrinter] => 0
        [isMapped] => 1
    )

[3] => Array
    (
        [PrinterID] => 4
        [PrinterName] => PRT04_GL
        [UserID] => 1
        [isDefaultPrinter] => 0
        [isMapped] => 0
    )       

Based on this example I tried the following:

uasort($myarray, 'sort_by_order');
function sort_by_order ($a, $b) {
  return $a['PrinterName'] - $b['PrinterName'];
}

This does not work. I get errors like "A non-numeric value encountered ...", of course because my values are strings and the function is for numeric values.

How do I sort the array?

Thank you!

EDIT: I think I did find the solution here. I used this and it seems to work:

uasort($myarray, 'sort_by_order');
function sort_by_order ( $a, $b ) { return strcmp($a['PrinterName'], $b['PrinterName']); }
StefanK
  • 23
  • 6

1 Answers1

-1

Please try with $a['PrinterName'] > $b['PrinterName']

It will compare the values

EitBiz
  • 11
  • 3