undefinedfix
Sign in

PHP usort sort in php7 . 2 and 5 . The results are different in version 6

MrBadonkadonk edited in Sun, 03 Jul 2022

Problem description

To sort the array, first sort the field a in positive order, but row 0 to the end, and then sort the field B in reverse order

Implementation code


// 对数组进行排序,先对a字段正序排序,但0排到最后,再对b字段逆序排序
$a1 = array(
    array('a' => 1,'b' => 1),
    array('a' => 2,'b' => 1),
    array('a' => 1,'b' => 2),
    array('a' => 0,'b' => 1),
    array('a' => 1,'b' => 3),
    array('a' => 2,'b' => 2),
    array('a' => 0,'b' => 2),
    array('a' => 2,'b' => 3)
);

// 对a排序
function my_sort1($a,$b) {
    if ($a['a'] == 0) {
        return 1;
    }
    if ($b['a'] == 0) {
        return -1;
    }
    if ($a['a'] > $b['a']) {
        return 1;
    }
    return -1;
}

// 对b排序
function my_sort2($a,$b) {
    if ($a['a'] == $b['a']) {
        if ($a['b'] < $b['b']) {
            return 1;
        } elseif ($a['b'] > $b['b']) {
            return -1;
        }
    }
    return 0;
}

usort($a1,"my_sort1");
usort2($a1,"my_sort2");

Return results

Php7.2 returns normal array results(

[0] => Array
    (
        [a] => 1
        [b] => 3
    )

[1] => Array
    (
        [a] => 1
        [b] => 2
    )

[2] => Array
    (
        [a] => 1
        [b] => 1
    )

[3] => Array
    (
        [a] => 2
        [b] => 3
    )

[4] => Array
    (
        [a] => 2
        [b] => 2
    )

[5] => Array
    (
        [a] => 2
        [b] => 1
    )

[6] => Array
    (
        [a] => 0
        [b] => 2
    )

[7] => Array
    (
        [a] => 0
        [b] => 1
    )

)Problem with php5.6 return result array(

[0] => Array
    (
        [a] => 0
        [b] => 2
    )

[1] => Array
    (
        [a] => 0
        [b] => 1
    )

[2] => Array
    (
        [a] => 2
        [b] => 3
    )

[3] => Array
    (
        [a] => 2
        [b] => 2
    )

[4] => Array
    (
        [a] => 2
        [b] => 1
    )

[5] => Array
    (
        [a] => 1
        [b] => 3
    )

[6] => Array
    (
        [a] => 1
        [b] => 2
    )

[7] => Array
    (
        [a] => 1
        [b] => 1
    )

)

reflection

**Check the relevant information, the function php7 did not modify the usort method. Question: whether the php7 version has modified the PHP array, if you have any God to know, I hope you can give me your advice! **

2 Replies
MTM
commented on Sun, 03 Jul 2022
if ($b['a'] == 0) {
    return -1;
}

Your if is redundant. The comparison results of two if will conflict, so it may produce unstable results in sorting.

I changed your code

<?php

// 对数组进行排序,先对a字段正序排序,但0排到最后,再对b字段逆序排序
$a1 = array(
    array('a' => 1,'b' => 1),
    array('a' => 2,'b' => 1),
    array('a' => 1,'b' => 2),
    array('a' => 0,'b' => 1),
    array('a' => 1,'b' => 3),
    array('a' => 2,'b' => 2),
    array('a' => 0,'b' => 2),
    array('a' => 2,'b' => 3)
);

// 对a排序
$count = 0;
function my_sort1($a,$b) {
    global $count;
    $count++;
    if ($a['a'] == 0) {
        return 1;
    }
    /*if ($b['a'] == 0) {
        return -1;
    }*/
    if ($a['a'] > $b['a']) {
        return 1;
    }
    return -1;
}

// 对b排序
function my_sort2($a,$b) {
    if ($a['a'] == $b['a']) {
        if ($a['b'] < $b['b']) {
            return 1;
        } elseif ($a['b'] > $b['b']) {
            return -1;
        }
    }
    return 0;
}

usort($a1,"my_sort1");
usort($a1,"my_sort2");

echo 'Compare times: '.$count,"\n";
var_export($a1);

When php7.2 does not comment the second if, it compares 15 times, when php5.6 does not comment 19 times, it compares 14 times

Braden
commented on Sun, 03 Jul 2022
function test($a, $b) {
    echo '当前a:' . implode(',', $a) . PHP_EOL;
    echo '当前b:' . implode(',', $b) . PHP_EOL;
    return 0;
}

Using this code to test, we can find that php5.6 and php7 read $a and $B in different order. We guess this problem is the cause

lock This question has been locked and the reply function has been disabled.