Duplicate array sorting in PHP

I need to sort an array in PHP and then sort it by the second index if the first index is the same. Let me explain:

Items will be randomly ordered:

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

I need to sort them first by first number. therefore, the result will be:

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

Now you can see that all elements are grouped by the first index together. Now I need to group the current grouping by the second index "WITHIN". So it looks like this:

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

It was just a representation of an 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
        )
)

It would be very helpful to help.

+3
source share
4 answers

, @casablanca, . , , .

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

, usort().

http://php.net/usort

+1

usort , , , :

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'];
}
+6

You can use the PHP 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');

In PHP 5.3, the following is possible:

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

You can make your comparative function as complex as you need it.

+1
source
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 the usort () function

http://php.net/usort

+1
source

Source: https://habr.com/ru/post/1777343/


All Articles