0

Through a series of SQL queries I have obtained an array. I can not do the sort in SQL but I have to sort the multidimensional array -

 Array
(
    [133] => Array
        (
            [pr] => 77.5062
            [service_pr] => 1
            [al] => 100
            [service_al] => 1
            [name] => item 1  
        )

    [134] => Array
        (
            [pr] => 79.60
            [service_pr] => 1
            [al] => 100
            [service_al] => 1
            [name] => item 2
        )
    [135] => Array
        (
            [pr] => 88.40
            [service_pr] => 1
            [al] => 100
            [service_al] => 1
            [name] => item 3
        )
)

How can I sort the array in ascending order on the key "pr"?

Dan
  • 8,983
  • 5
  • 37
  • 71
user1377059
  • 21
  • 2
  • 7

1 Answers1

3

Use usort function:

usort($array, function($a, $b) { return $a['pr'] - $b['pr']; });

If you want descending order just replace $a with $b and $b with $a:

usort($array, function($a, $b) { return $b['pr'] - $a['pr']; });

Live preview

Lkopo
  • 4,560
  • 8
  • 32
  • 59