2

I have the following array

$infoArray = Array
(
    [Shockwafe Ultra 9.2] => Array
        (
            [0] => 8
            [1] => 45 Hz - 18.000 Hz
            [2] => 9.2
            [3] => 1
            [4] => 1
            [5] => 1
            [6] => 
            [7] => 
            [8] => B+
            [9] => B+
            [10] => A
            [11] => B
        )

    [Bose 5.1 SoundTouch 300] => Array
        (
            [0] => 9
            [1] => 45 Hz - 20.000 Hz
            [2] => 5.1
            [3] => 1
            [4] => 1
            [5] => 1
            [6] => 1
            [7] => 1
            [8] => A
            [9] => A
            [10] => A+
            [11] => A+
        )

    [Sonos 5.1 System] => Array
        (
            [0] => 10
            [1] => 31.5 Hz - 20.000 Hz
            [2] => 5.1
            [3] => 1
            [4] => 1
            [5] => 1
            [6] => 1
            [7] => 1
            [8] => A+
            [9] => A+
            [10] => A+
            [11] => A+
        )

)

I am trying to sort it with the use of PHP. I want to sort by the [0] value in each array member. So the order should be, Sonos first, Bose second, Shockwafe third.

My PHP code looks as following:

function sortByOrder($a, $b) {
return $a[0] - $b[0]; }

uasort($infoArray, 'sortByOrder');

I've tried this on wamp, where it succesfully arranges the order like i'd want. However, online in my Wordpress installation, this does NOT work. It simply does not re-arrange the array. It should be noted, that I am working inside functions.php within a shortcode. I tried moving the sortByOrder() function outside of the shortcode area, with no success.

Any ideas what goes wrong?

  • Try adding a `die()` statement in your comparison function to see if it's being called first of all. Then do two print_r statement on the array, one before and one after so we can see what is happening if anything. Also, check your logs for any errors. – obsirdian Dec 22 '17 at 18:02
  • When sorting by first column and the subarrays have the same length, `sort($array)` will do. – mickmackusa Apr 14 '22 at 22:21

2 Answers2

3
function sortByOrder($a, $b) {
        $result = 0;
        if ($b['rating'] > $a['rating']) {
            $result = 1;
        } elseif ($b['rating'] < $a['rating']) {
            $result = -1;
        }
        return $result; 
   }

This ended up being my solution. It has something to do with decimal numbers, I'm not exactly sure why it worked. Solution in OP is fine if you work with whole numbers, but this is required for decimal numbers.

0

Ran into the same issue. Turns out the comparison callback is expected to return an integer value. If you return a float, your value will be casted to an int. This means that values between -1 and 1 all become 0. That's why whole numbers work: the difference between them is always at least 1 if they aren't the same.

PHP 7.4 introduced arrow functions and the "spaceship operator". Those make comparator functions very concise. Ordering "by rating" would therefore be:

uasort($infoArray, fn($a, $b) => $a['rating'] <=> $b['rating']);
Roman
  • 5,520
  • 22
  • 44