Summary of how to use various sorting functions in php

伊谢尔伦
Release: 2023-03-11 14:02:01
Original
1529 people have browsed it

If you have been using PHP for a while, you should already be familiar with its arrays - this data structure allows you to store multiple values ​​in a single variable and use them as Operate on a collection.

Often, developers find it useful to sort values ​​or array elements using this data structure in PHP. PHP provides some sorting functions for a variety of arrays. These functions allow you to arrange elements within the array and also allow you to reorder them in many different ways. In this article we will discuss some of the most important functions in this sorting.

Simple sorting

First, let’s take a look at the simplest case: simply sorting an array element from low to high. This function can be used both numerically and The size arrangement can also be arranged alphabetically. PHP's sort() function implements this function, as shown in Listing A:

Listing A

<?php
$data = array(5,8,1,7,2);
sort($data);
print_r($data);
?>
Copy after login

The output result is as follows:

Array ([0] => 1
[1] => 2
[2] => 5
[3] => 7
[4] => 8
)
Copy after login

You can also use the rsort() function for sorting. Its result is opposite to the simple sorting result of sort() used previously. The Rsort() function sorts the array elements from high to low, either numerically or alphabetically. Listing B shows us an example of it:

Listing B

<?php 
$data = array(5,8,1,7,2);
rsort($data);
print_r($data);
?>
Copy after login

Its output is as follows:

Array ([0] => 8
[1] => 7
[2] => 5
[3] => 2
[4] => 1
)
Copy after login

Sort according to keywords

When we use arrays, we often reorder the array according to keywords, from high to low. The Ksort() function is a function that sorts according to keywords. At the same time, it maintains the relevance of keywords during the sorting process. Listing C is an example:

Listing C

<?php 
$data = array("US" => "United States", "IN" => "India", "DE" => "Germany", "ES" => "Spain");
ksort($data); 
print_r($data);
?>
Copy after login

Its output is as follows:

Array ([DE] => Germany
[ES] => Spain
[IN] => India
[US] => United States
)
Copy after login

The Krsort() function is based on The keyword inverts the array. Listing D is an example of this:

Listing D

<?php 
$data = array("US" => "United States", "IN" => "India", "DE" => "Germany", "ES" => "Spain");
krsort($data); 
print_r($data);
?>
Copy after login

Its output is as follows:

Array ([US] => United States
[IN] => India
[ES] => Spain
[DE] => Germany
)
Copy after login

Sort by value

If you want to use value sorting instead of keyword sorting, PHP can also meet your requirements. You just need to use the asort() function instead of the ksort() function mentioned earlier. As shown in Listing E:

Listing E

<?php 
$data = array("US" => "United States", "IN" => "India", "DE" => "Germany", "ES" => "Spain");
asort($data); 
print_r($data);
?>
Copy after login

The following is its output. Note the difference between this result and the result obtained using the ksort() function above - in both cases, the sorting is alphabetical, but they are sorted according to different fields of the array.

同时,请注意关键字-值之间的联系会始终保持;它只是关键字-值对排序后的一种方式,排序并不会改变它们的对应关系。

Array ([DE] => Germany
[IN] => India
[ES] => Spain
[US] => United States
)
Copy after login

现在,你肯定能猜到这种排序也可以进行倒排,它使用arsort()函数完成这个功能。Listing F就是一个例子:

Listing F

<?php 
$data = array("US" => "United States", "IN" => "India", "DE" => "Germany", "ES" => "Spain");
arsort($data); 
print_r($data);
?>
Copy after login

下面是它的输出结果,根据值按字母表顺序进行倒排。将下面的结果与用krsort()函数进行倒排后生成的结果进行比较,就能很容易明白两者的不同了。

Array ([US] => United States
[ES] => Spain
[IN] => India
[DE] => Germany
)
Copy after login

自然语言排序

PHP有一个非常独特的排序方式,这种方式使用认知而不是使用计算规则。这种特性称为自然语言排序,当创建模糊逻辑应用软件的时候这种排序方式非常有用。下面大家可以来看看它的一个简单例子,如Listing G所示:

Listing G

<?php 
$data = array("book-1", "book-10", "book-100", "book-5"); 
sort($data);
print_r($data);
natsort($data); 
print_r($data);
?>
Copy after login
Copy after login

它的输出结果如下:

Array ([0] => book-1
[1] => book-10
[2] => book-100
[3] => book-5
)
Array
(
[0] => book-1
[3] => book-5
[1] => book-10
[2] => book-100
)
Copy after login
Copy after login

它们的不同已经很清楚了:第二个排序结果更直观,更“人性化”,然而第一个则更符合算法规则,更具“计算机”特点。

自然语言能进行倒排吗?答案是肯定的!只要对natsort()的结果使用array_reverse()函数就可以了,Listing H就是一个简单例子:

Listing H

<?php 
$data = array("book-1", "book-10", "book-100", "book-5");
natsort($data); 
print_r(array_reverse($data));
?>
Copy after login
Copy after login

下面是它的输出结果:

Array ([0] => book-100
[1] => book-10
[2] => book-5
[3] => book-1
)
Copy after login
Copy after login

根据用户自定义的规则排序

PHP也能让你定义自己的排序算法,你可以通过创建你自己的比较函数,并把它传递给usort()函数。如果第一个参数比第二个参数“小”的话,比较函数必须返回一个比0小的数,如果第一参数比第二个参数“大”的话,比较函数应该返回一个比0大的数。

Listing I就是这样的一个例子,在这个例子中根据它们的长度对数组元素进行排序,最短的项放在最前面:

Listing I

<?php 
$data = array("joe@host.com", "john.doe@gh.co.uk", "asmithsonian@us.info", "jay@zoo.tw");
usort($data, &#39;sortByLen&#39;);
print_r($data); 
function sortByLen($a, $b) {
if (strlen($a) == strlen($b)) {
return 0;
} else {
return (strlen($a) > strlen($b)) ? 1 : -1;
}
}
?>
Copy after login
Copy after login

这样,就创建了我们自己的比较函数,这个函数使用strlen()函数比较每一个字符串的个数,然后分别返回1,0或-1.这个返回值是决定元素排列的基础。下面是它的输出结果:

Array ([0] => jay@zoo.tw
[1] => joe@host.com
[2] => john.doe@gh.co.uk
[3] => asmithsonian@us.info
)
Copy after login
Copy after login

自然语言排序

PHP有一个非常独特的排序方式,这种方式使用认知而不是使用计算规则。这种特性称为自然语言排序,当创建模糊逻辑应用软件的时候这种排序方式非常有用。下面大家可以来看看它的一个简单例子,如Listing G所示:

Listing G

<?php 
$data = array("book-1", "book-10", "book-100", "book-5"); 
sort($data);
print_r($data);
natsort($data); 
print_r($data);
?>
Copy after login
Copy after login

它的输出结果如下:

Array ([0] => book-1
[1] => book-10
[2] => book-100
[3] => book-5
)
Array
(
[0] => book-1
[3] => book-5
[1] => book-10
[2] => book-100
)
Copy after login
Copy after login

它们的不同已经很清楚了:第二个排序结果更直观,更“人性化”,然而第一个则更符合算法规则,更具“计算机”特点。

自然语言能进行倒排吗?答案是肯定的!只要对natsort()的结果使用array_reverse()函数就可以了,Listing H就是一个简单例子:

Listing H

<?php 
$data = array("book-1", "book-10", "book-100", "book-5");
natsort($data); 
print_r(array_reverse($data));
?>
Copy after login
Copy after login

下面是它的输出结果:

Array ([0] => book-100
[1] => book-10
[2] => book-5
[3] => book-1
)
Copy after login
Copy after login

根据用户自定义的规则排序

PHP也能让你定义自己的排序算法,你可以通过创建你自己的比较函数,并把它传递给usort()函数。如果第一个参数比第二个参数“小”的话,比较函数必须返回一个比0小的数,如果第一参数比第二个参数“大”的话,比较函数应该返回一个比0大的数。

Listing I就是这样的一个例子,在这个例子中根据它们的长度对数组元素进行排序,最短的项放在最前面:

Listing I

<?php 
$data = array("joe@host.com", "john.doe@gh.co.uk", "asmithsonian@us.info", "jay@zoo.tw");
usort($data, &#39;sortByLen&#39;);
print_r($data); 
function sortByLen($a, $b) {
if (strlen($a) == strlen($b)) {
return 0;
} else {
return (strlen($a) > strlen($b)) ? 1 : -1;
}
}
?>
Copy after login
Copy after login

这样,就创建了我们自己的比较函数,这个函数使用strlen()函数比较每一个字符串的个数,然后分别返回1,0或-1.这个返回值是决定元素排列的基础。下面是它的输出结果:

Array ([0] => jay@zoo.tw
[1] => joe@host.com
[2] => john.doe@gh.co.uk
[3] => asmithsonian@us.info
)
Copy after login
Copy after login

多维排序

最后,PHP也允许在多维数组上执行一些比较复杂的排序——例如,首先对一个嵌套数组使用一个普通的关键字进行排序,然后再根据另一个关键字进行排序。这与使用SQL的ORDER BY语句对多个字段进行排序非常相似。为了能更好的明白它是如何工作的,请仔细看Listing J所举的例子:

Listing J

<?php 
$data = array(array("id" => 1, "name" => "Boney M", "rating" => 3),
array("id" => 2, "name" => "Take That", "rating" => 1),
array("id" => 3, "name" => "The Killers", "rating" => 4),
array("id" => 4, "name" => "Lusain", "rating" => 3),
); 
foreach ($data as $key => $value) {
$name[$key] = $value[&#39;name&#39;];
$rating[$key] = $value[&#39;rating&#39;];
}
array_multisort($rating, $name, $data); 
print_r($data);
?>
Copy after login

这里,我们在$data数组中模拟了一个行和列数组。然后,我使用array_multisort()函数对数据集合进行重排,首先是根据rating进行排序,然后,如果rating相等的话,再根据name排序。它的输出结果如下:

Array ([0] => Array
(
[id] => 2
[name] => Take That
[rating] => 1
) 
[1] => Array
(
[id] => 1
[name] => Boney M
[rating] => 3
)
[2] => Array
(
[id] => 4
[name] => Lusain
[rating] => 3
)
[3] => Array
(
[id] => 3
[name] => The Killers
[rating] => 4
)
)
Copy after login

array_multisort()函数是PHP中最有用的函数之一,它有非常广泛的应用范围。另外,就如你在例子中所看到的,它能对多个不相关的数组进行排序,也可以使用其中的一个元素作为下次排序的基础,还可以对数据库结果集进行排序。

这些例子应该让你对PHP中各种数组排序函数的使用有了初步的了解,也向你展示了一些隐藏在PHP数组处理工具包的内部功能。


The above is the detailed content of Summary of how to use various sorting functions in php. 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
Popular Tutorials
More>
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!