Detailed explanation of php sorting function (with examples)

王林
Release: 2023-04-07 11:12:02
Original
4981 people have browsed it

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);
Copy after login

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);
Copy after login
"; }?>
Copy after login
1、使用array_multisort 使用这个方法,会比较麻烦些,要将age提取出来存储到一维数组里,然后按照age升序排列。具体代码如下: 代码如下: $bl) ? -1 : 1; }); 这里使用了匿名函数,如果有需要也可以单独提取出来。其中$a, $b可以理解为$users数组下的元素,可以直接索引name值, 并计算长度,而后比较长度就可以了。
Copy after login

以上是工作中常用的数组排序方法,想了解更多相关问题请访问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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!