Home >Backend Development >PHP Tutorial >Detailed explanation of php sorting function (with examples)

Detailed explanation of php sorting function (with examples)

王林
王林Original
2019-08-31 14:36:205139browse

Detailed explanation of php sorting function (with examples)

There are many ways to sort. Usually, the array sorting functions we often use are: sort(), rsort(), asort(), arsort(), ksort(), krsort(). The following is a detailed introduction for everyone.

sort(array,sortingtype);

Detailed explanation of php sorting function (with examples)

0 = SORT_REGULAR - Default. Put each item in regular order (Standard ASCII, no change of type)

1 = SORT_NUMERIC - Treat each item as a number.

2 = SORT_STRING - Treat each item as a string.

3 = SORT_LOCALE_STRING - Treat each item as a string, based on the current locale (can be changed via setlocale()).

4 = SORT_NATURAL - Treat each item as a string, using natural sorting like natsort().

5 = SORT_FLAG_CASE - Strings can be sorted in combination (bitwise OR) with SORT_STRING or SORT_NATURAL, case-insensitively.

Definition and usage:

sort() function sorts the index array in ascending order.

rsort() function sorts the index array in descending order, and other usages are the same.

Note: This function assigns a new key name to the unit in the array. The original key name will be deleted.

Returns TRUE if successful, otherwise returns FALSE.

Tip: Please use the rsort() function to sort the index array in descending order.

The sort() function is used to sort array cells from low to high.

rsort() function is used to sort array cells from high to low.

asort() function is used to sort the array cells from low to high and maintain the index relationship.

The PHP asort() function is used to sort the array cells from low to high and maintain the index relationship. It returns TRUE if successful and FALSE if failed.

The arsort() function is used to sort the array cells from high to low and maintain the index relationship.

PHP arsort() function behaves opposite to asort(). It sorts the array cells from high to low and maintains the index relationship. Please refer to the use of asort() function.

ksort() function is used to sort array cells from low to high by key name.

PHP ksort() function is used to sort array cells from low to high by key name. It returns TRUE if successful and FALSE if failed. This function retains the original key name, so it is often used in associative arrays.

krsort() 函数用于对数组单元按照键名从高到低进行排序。

PHP krsort() 函数行为与 ksort() 相反,对数组单元按照键名进行由高到低排序,请参考 ksort() 函数使用。

使用用户自定义的比较函数对数组 $a 中的元素进行排序:

usort(array,myfunction);
<?php
function my_sort($a,$b){
   if ($a==$b) return 0;
   return ($a<$b)?-1:1;
}

$a=array(4,2,8,6);
usort($a,"my_sort");

$arrlength=count($a);
for($x=0;$x<$arrlength;$x++)
   {
      echo $a[$x];
      echo "<br>";
   }?>
1、使用array_multisort

使用这个方法,会比较麻烦些,要将age提取出来存储到一维数组里,然后按照age升序排列。具体代码如下:

代码如下:<?php
$ages = array();
foreach ($users as $user) {
  $ages[] = $user[&#39;age&#39;];
}
array_multisort($ages, SORT_ASC, $users);

执行后,$users就是排序好的数组了,可以打印出来看看。如果需要先按年龄升序排列,再按照名称升序排列,方法同上,就是多提取一个名称数组出来,最后的排序方法这样调用:

代码如下:
array_multisort($ages, SORT_ASC, $names, SORT_ASC, $users);
2、使用usort
使用这个方法最大的好处就是可以自定义一些比较复杂的排序方法。例如按照名称的长度降序排列:

代码如下:
<?php
      usort($users, function($a, $b) {
      $al = strlen($a[&#39;name&#39;]);
      $bl = strlen($b[&#39;name&#39;]);
      if ($al == $bl) 
          return 0;      
          return ($al > $bl) ? -1 : 1;
    });

这里使用了匿名函数,如果有需要也可以单独提取出来。其中$a, $b可以理解为$users数组下的元素,可以直接索引name值,
并计算长度,而后比较长度就可以了。

以上是工作中常用的数组排序方法,想了解更多相关问题请访问PHP中文网:PHP视频教程

The above is the detailed content of Detailed explanation of php sorting function (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn