开发者

Double sorting an array in PHP

开发者 https://www.devze.com 2023-01-27 13:21 出处:网络
I need to sort an array开发者_开发问答 inPHP and then sort by its second index if the first index is the same. Let me explain :

I need to sort an array开发者_开发问答 in PHP and then sort by its second index if the first index is the same. Let me explain :

The elements would be randomly arranged :

(2,1),(4,4),(2,9),(4,8),(2,35),(2,1),(2,35),(4,4),(4,25),(4,4)

I would need to sort them by the first number first. so the result would be :

(2,1),(2,9),(2,1),(2,35),(4,4),(4,8),(4,4),(4,25),(4,4)

Now you can see all the elements are grouped by the first index together. Now i need to group by the second index "WITHIN" the current grouping. Thus make it look like :

(2,1),(2,1),(2,9),(2,35),(4,4),(4,4),(4,4),(4,8),(4,25)

This was just a simple representation of the array. The actual array is below :

Array
(
    [0] => Array
        (
            [practice_id] => 119
            [practice_location_id] => 173
        )

    [1] => Array
        (
            [practice_id] => 2
            [practice_location_id] => 75
        )

    [2] => Array
        (
            [practice_id] => 18
            [practice_location_id] => 28
        )

    [3] => Array
        (
            [practice_id] => 119
            [practice_location_id] => 174
        )

    [4] => Array
        (
            [practice_id] => 119
            [practice_location_id] => 173
        )

    [5] => Array
        (
            [practice_id] => 2
            [practice_location_id] => 75
        )

    [6] => Array
        (
            [practice_id] => 119
            [practice_location_id] => 174
        )

    [7] => Array
        (
            [practice_id] => 18
            [practice_location_id] => 28
        )

    [8] => Array
        (
            [practice_id] => 18
            [practice_location_id] => 27
        )
)

Would really appreciate some help.


Use usort with a custom comparison function, which sorts by the first field, but if they are equal, sorts by the second field:

function my_compare($a, $b) {
  if ($a['practice_id'] == $b['practice_id'])
    return $a['practice_location_id'] - $b['practice_location_id'];
  else
    return $a['practice_id'] - $b['practice_id'];
}


function compare_callback($x, $y) {
    if ($x['practice_id'] < $y['practice_id'])
        return 1;
    if ($x['practice_id'] > $y['practice_id'])
        return -1;
    if ($x['practice_id'] == $y['practice_id']) {
        if ($x['practice_location_id'] < $y['practice_location_id'])
            return 1;
        if ($x['practice_location_id'] > $y['practice_location_id'])
            return -1;
        if ($x['practice_location_id'] == $y['practice_location_id'])
            return 0;
    }
}

Use this method with usort()

http://php.net/usort


This callback uses what I just learned from @casablanca's answer, and puts it in a loop. This way, it won't just compare two, but as many keys there are to compare.

function sort_callback($x, $y)
    foreach ($x as $key => $value) {
        if ($x[$key] != $y[$key])
            return $x[$key] - $y[$key];
    }
    return 0;
}

As already stated, give this function as a callback to usort().

http://php.net/usort


You can use PHP's usort()-function:

function someCompareFunction($a, $b) {
    $order = array('practice_id', 'practice_location_id');

    foreach ($order as $key) {
        if (isset($a[$key]) && isset($b[$key]) && $a[$key] != $b[$key]) {
            return $a[$key] - $b[$key];
        }
    }

    return 0;
}

usort($yourArray, 'someCompareFunction');

The following is possible as of PHP 5.3:

usort($yourArray, function($a, $b) {
    // comparison code
});

You can make your compare-function as complex as you need it.

0

精彩评论

暂无评论...
验证码 换一张
取 消