php usort_使用PHP usort()简化复杂数据搜索

这篇博客探讨了在处理信息处理任务时对数据集进行排序的重要性,特别是在需要找到数组中的最小值或最大值时。PHP提供了多种内置排序函数,如min()和max(),但在面对复杂数据结构如多维数组或对象数组时,需要使用usort(),uasort()和uksort()等函数。usort()允许通过回调函数根据数组元素内的子元素进行排序,提供了极大的灵活性。博客还通过实例展示了如何根据不同属性对对象数组进行排序,并讨论了如何在排序时保留数字键的值,以及如何处理自然顺序和不区分大小写的排序问题。
摘要由CSDN通过智能技术生成

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值