这个usort cmp函数是如何工作的呢?

时间:2022-08-25 17:26:44

There's an answer here: Combine two array and order this new array by date

这里有一个答案:合并两个数组并按日期对这个新数组排序

It explains how two arrays can be merged and then sorted by date.

它解释了如何合并两个数组,然后按日期排序。

function cmp($a, $b){
    $ad = strtotime($a['date']);
    $bd = strtotime($b['date']);
    return ($ad-$bd);
}
$arr = array_merge($array1, $array2);
usort($arr, 'cmp');

the solution looks quite elegant, but I'm confused by

这个解决方案看起来很优雅,但我感到困惑

return ($ad-$bd);

I mean there's no comparison operator, it just subtracts

我的意思是没有比较运算符,它只是减法。

function newFunc($a, $b) {
    return($a-$b);
}

echo newFunc(5,3);

returns 2

返回2

So how does this actually indicate how to sort the arrays?

这是如何表示数组排序的呢?

Update:

更新:

I read further the documentation on the usort page as suggested. Does it perform this subtraction with each of the elements? Does it iterate through each array element and subtracts it from other elements? Just trying to wrap my head around this.

我进一步阅读了usort页面上的文档。它对每个元素都进行减法吗?它是否遍历每个数组元素并将其从其他元素中减去?我只是想把我的脑袋绕过去。

2 个解决方案

#1


2  

To quote the documentation, a usort's value_compare_func is a function that:

引用文档,usort的value_compare_func函数是:

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

如果第一个参数被认为分别小于、等于或大于第二个参数,则比较函数必须返回一个小于、等于或大于零的整数。

In this case, both dates are converted to a Unix timestamp, i.e., the number of seconds since the epoch. If $a comes before $b, it will have less seconds since the epoch, so subtracting it from $b will return a negative number. If it comes after $b, subtracting the two will return a positive number, and if they are the same, the subtraction will of course return zero.

在本例中,两个日期都被转换为Unix时间戳,即。,表示自纪元以来的秒数。如果$a在$b之前,那么从纪元开始,它的秒数将减少,所以从$b中减去它将返回一个负数。如果它在b美元之后,减去这两个就会得到一个正数,如果它们是一样的,减法当然会返回0。

#2


2  

If you read a manual, you see:

如果你读一本手册,你会看到:

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

如果第一个参数被认为分别小于、等于或大于第二个参数,则比较函数必须返回一个小于、等于或大于零的整数。

By substracting values you get either positive or negative or 0 value, which allows to sort values.

通过对值进行减法,可以得到正的或负的或0值,这允许排序值。

#1


2  

To quote the documentation, a usort's value_compare_func is a function that:

引用文档,usort的value_compare_func函数是:

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

如果第一个参数被认为分别小于、等于或大于第二个参数,则比较函数必须返回一个小于、等于或大于零的整数。

In this case, both dates are converted to a Unix timestamp, i.e., the number of seconds since the epoch. If $a comes before $b, it will have less seconds since the epoch, so subtracting it from $b will return a negative number. If it comes after $b, subtracting the two will return a positive number, and if they are the same, the subtraction will of course return zero.

在本例中,两个日期都被转换为Unix时间戳,即。,表示自纪元以来的秒数。如果$a在$b之前,那么从纪元开始,它的秒数将减少,所以从$b中减去它将返回一个负数。如果它在b美元之后,减去这两个就会得到一个正数,如果它们是一样的,减法当然会返回0。

#2


2  

If you read a manual, you see:

如果你读一本手册,你会看到:

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

如果第一个参数被认为分别小于、等于或大于第二个参数,则比较函数必须返回一个小于、等于或大于零的整数。

By substracting values you get either positive or negative or 0 value, which allows to sort values.

通过对值进行减法,可以得到正的或负的或0值,这允许排序值。