php usort
Introduction
介绍
Many of the most common information processing tasks require sorting data sets. For example, you may want to find the largest or smallest value in a collection. Or you may want to order the data set in numeric or alphabetical order. The correct data structure for this kind of work is the array. Fortunately PHP provides an excellent collection of built-in array sorting functions. These functions can re-arrange arrays in ascending or descending sequence. They can preserve or renumber the keys. They can even sort in "natural order" just like a human being would sort a collection, ignoring case-sensitivity in the data set. And if everything you need to sort can be expressed in a one-dimensional array, one of the PHP built-in array sorting functions will surely do the trick.
许多最常见的信息处理任务都需要对数据集进行排序。 例如,您可能想在集合中找到最大值或最小值。 或者,您可能想按数字或字母顺序对数据集进行排序。 这类工作的正确数据结构是数组 。 幸运的是,PHP提供了出色的内置数组排序功能集合 。 这些函数可以按升序或降序重新排列数组。 他们可以保留或重新编号密钥。 他们甚至可以按照“自然顺序”进行排序,就像人类对集合进行排序一样,而忽略了数据集中区分大小写的情况。 而且,如果您需要排序的所有内容都可以用一维数组表示,那么PHP内置数组排序函数之一肯定可以解决问题。
Finding the Minimum and Maximum Values in a One-Dimensional Array
If you need to find a single min/max value, PHP has built-in functions to do the work for you. For this simple task, you do not need to write any code. Just call the PHP function that suits your needs. Choose either min() or max().
如果您需要找到一个最小/最大值,PHP具有内置函数可以为您完成工作。 对于此简单任务,您无需编写任何代码。 只需调用适合您需求PHP函数即可。 选择min()或max() 。
But if there are any "interesting" edge cases, you may want to read the man pages carefully. Because PHP is a loosely typed language, these comparisons are done under rules of type-coercion. PHP will interpret the values in ways that allows PHP to make a duck-typed comparison. Which value do you think would be the minimum among these array elements:
但是,如果有任何“有趣的”极端情况,您可能需要仔细阅读手册页。 因为PHP是一种松散类型的语言,所以这些比较是在类型强制的规则下进行的。 PHP将以允许PHP进行鸭式比较的方式来解释值。 您认为哪个值是这些数组元素中的最小值:
$arr = [2, '2', Two];
http://php.net/manual/en/language.types.boolean.php
http://php.net/manual/zh/language.types.boolean.php
http://php.net/manual/en/language.operators.comparison.php
http://php.net/manual/en/language.operators.comparison.php
http://php.net/manual/en/types.comparisons.php
http://php.net/manual/zh/types.comparisons.php
http://php.net/manual/en/language.types.type-juggling.php
http://php.net/manual/zh/language.types.type-juggling.php
http://php.net/manual/en/language.types.string.php#language.types.string.conversion
http://php.net/manual/en/language.types.string.php#language.types.string.conversion
Finding some Minimum and Maximum Values in a One-Dimensional Array
在一维数组中查找
If you need to find more than a single min/max value, you need to sort the array. You can sort in either ascending or descending order, then you can get a collection of the largest or smallest values with array_slice(). Once you have sorted the array, you can return the array in reverse order (you do not need to sort again) with array_reverse().
如果需要查找多个单个的最小值/最大值,则需要对数组进行排序。 您可以按升序或降序排序,然后可以使用array_slice()获得最大值或最小值的集合。 对数组进行排序后,可以使用array_reverse()以相反的顺序返回数组(无需再次排序) 。
Using Multidimensional Arrays or Arrays of Objects
使用多维数组或对象数组
If you have a complex data structure, perhaps an array consisting of "sub-arrays" or objects, you cannot use the built-in PHP sorting functions. PHP cannot directly compare an array to another array or an object to another object, so it will be necessary to write some code. For these kinds of complex sorts, PHP provides the