首页  >  文章  >  后端开发  >  PHP 中的排序

PHP 中的排序

WBOY
WBOY原创
2024-08-29 13:04:46306浏览

排序是将数组的元素按照特定的顺序排列。 PHP 对普通数组(如数值数组)和关联数组执行排序。像数字数组这样的普通数组可以使用简单的 sort() 函数进行排序,并且为了对关联数组执行排序,我们有不同的函数。

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

排序可以按升序或降序、字母或数字顺序、自然方式、随机以及用户定义的顺序进行。对于数值数组或索引数组等数组以及关联数组,排序是基于键或基于任意两个顺序(例如升序或降序)中的值按升序或降序数组进行排序。如果数据元素采用排序形式,则对数组进行排序可以使您的搜索更加轻松。

PHP 中如何执行排序?

排序是在 PHP 中使用排序函数执行的。有多种排序功能。

假设您想根据资历了解家庭成员的年龄。一个家庭可能有 15 名成员。为了对 15 名成员的年龄进行排序,我们使用排序函数并快速得到结果。因此,在这种情况下,排序就显得尤为重要。

此外,不需要使用任何库。

语法:

sort(array);

其中 array 是输入数组的名称。

以下示例对 $people 和 $ages 数组的元素进行排序。

按字母顺序排序:

$people = array ('Rama', 'James', 'Mary', 'Alice', 'Radha');

按数字顺序排序:

$ages = array (25,10,30,15,20);

组合以上两个数组并创建一个关联。

$people_ages = array ('James' => 25, 'Rama' => 10, 'Mary' => 30, 'Alice' => 15, 'Radha' => 20);

按数字顺序排序,示例:

代码:

<?php
//example to perform ages array
$ages = array(25,10,30,15,20);
// calculate length of array
$array_length = count($ages);
echo "Before Sort"."<br>";
//array before sorting
for($i=0;$i<$array_length;$i++)
{
echo $ages[$i] ."<br>";
}
echo '<hr>';
//performing sort
sort($ages);
echo "After Sort"."<br>";
//array after sorting
for($i=0;$i<$array_length;$i++)
{
echo $ages[$i]."<br>";
} ?>

输出:

PHP 中的排序

按字母顺序排序,示例:

代码:

<?php
//example to perform people array
$people= array('Rama', 'James', 'Mary', 'Alice', 'Radha');
// calculate length of array
$array_length = count($people);
echo "Before Sort"."<br>";
//array before sorting
for($i=0;$i<$array_length;$i++)
{
echo $people[$i] ."<br>";
}
echo '<hr>';
//performing sort
sort($people);
echo "After Sort"."<br>";
//array after sorting
for($i=0;$i<$array_length;$i++)
{
echo $people[$i]."<br>";
}
?>

输出 :

PHP 中的排序

对关联数组进行排序

对具有键值对关联的关联数组执行排序将导致键丢失。此外,虽然执行了排序,但数组的每个元素现在都被分配了一个新的数字索引。

代码:

// example to perform sort on people and ages array together
// you will find that the keys are not preserved and changed
$people_ages = array('James' => 25, 'Rama' => 10, 'Mary' => 30, 'Alice' => 15, 'Radha' => 20);
// calculate length of array
$array_length = count($people_ages);
echo "Before Sort"."<br>";
//array before sorting we will use foreach loop
foreach($people_ages as $key=>$value)
{
echo $key."=>".$value."<br>";
}
echo '<hr>';
//performing sort
sort($people_ages);
echo "After Sort"."<br>";
//array after sorting
foreach ($people_ages as $key=>$value)
{
echo $key."=>".$value."<br>";
}

输出:

PHP 中的排序

因此,我们使用 asort(),而不是简单的 sort()。 asort() 是一个按升序对关联数组的元素进行排序的函数。 arsort() 是一个对数组元素进行降序排序的函数。两者均按值排序。现在让我们详细了解这些数组以及其他数组函数

PHP 中的排序类型

下面提到了不同类型的数组函数,以及排序的顺序,无论是升序还是降序,以及函数按键排序或按值排序。

  • sort(): this function sorts the input array in ascending order and sorts it by value
  • rsort(): this function sorts the input array in descending order and sorts it by value
  • asort(): this function sorts the input array in ascending order and sorts it by value
  • arsort(): this function sorts the input array in descending order and sorts by value
  • ksort(): this function sorts the input array in ascending order and sorts it by key
  • krsort(): this function sorts the input array in descending order and sorts it by key
  • usort(): this function sorts the input array based on the user-defined function and sorts by value
  • uasort(): this function sorts the input array based on the user-defined function and sorts by value
  • uksort(): this function sorts the input array based on the user-defined function and sorts by key
  • natsort(): this function sorts the input array-based natural ordering.
  • natcasesort(): this function sorts the input array-based natural ordering and is case insensitive.
  • shuffle(): this function sorts the input array based on the value, and the output is a random order of values.

Let us learn about each function in detail

1. sort()

This function we have already seen. This function performs sorting on the given array and arranges the elements of the array in ascending array.

Code :

//example to perform ages array
$ages = array(25,10,30,15,20);
// calculate length of array
$array_length = count($ages);
echo "Before Sort"."<br>";
//array before sorting
for($i=0;$i<$array_length;$i++)
{
echo $ages[$i] ."<br>";
}
echo '<hr>';
//performing sort
sort($ages);
echo "After Sort"."<br>";
//array after sorting
for($i=0;$i<$array_length;$i++)
{
echo $ages[$i]."<br>";
}

Output:

PHP 中的排序

2. rsort()

This function performs sorting on the given array and arranges the elements of the array in descending array, opposite of what sort() function does. Also, the sorting is performed with values.

a. Code:

//example to perform ages array
$ages = array(25,10,30,15,20);
// calculate length of array
$array_length = count($ages);
echo "Before Sort"."<br>";
//array before sorting
for($i=0;$i<$array_length;$i++)
{
echo $ages[$i] ."<br>";
}
echo '<hr>';
//performing sort
rsort($ages);
echo "After Sort"."<br>";
//array after sorting
for($i=0;$i<$array_length;$i++)
{
echo $ages[$i]."<br>";
}

Output :

PHP 中的排序

b. Code:

//example to perform people array
$people= array('Rama', 'James', 'Mary', 'Alice', 'Radha');
// calculate length of array
$array_length = count($people);
echo "Before Sort"."<br>";
//array before sorting
for($i=0;$i<$array_length;$i++)
{
echo $people[$i] ."<br>";
}
echo '<hr>';
//performing sort
rsort($people);
echo "After Sort"."<br>";
//array after sorting
for($i=0;$i<$array_length;$i++)
{
echo $people[$i]."<br>";
}

Output:

PHP 中的排序

3. asort()

This function performs sorting on the given array and arranges the array’s values in ascending order, opposite of what sort() function does. Also, the sorting is performed with values and not keys.

Code :

//example to perform people_ages array
$people_ages = array('James' => 25, 'Rama' => 10, 'Mary' => 30, 'Alice' => 15, 'Radha' => 20);
// calculate length of array
$array_length = count($people_ages);
echo "Before Sort"."<br>";
//array before sorting
foreach($people_ages as $key=>$value)
{
echo $key."=>".$value."<br>";
}
echo '<hr>';
//performing sort
asort($people_ages);
echo "After Sort"."<br>";
//array after sorting
foreach($people_ages as $key=>$value)
{
echo $key."=>".$value."<br>";
}

Output:

PHP 中的排序

4. arsort()

This function performs sorting on the given array and arranges the array’s values in a descending array. This example prints the array using a foreach loop and outputs the result as before sorting and after sorting.

Code:

//example to perform people_ages array
$people_ages = array('James' => 25, 'Rama' => 10, 'Mary' => 30, 'Alice' => 15, 'Radha' => 20);
// calculate length of array
$array_length = count($people_ages);
echo "Before Sort"."<br>";
//array before sorting
foreach($people_ages as $key=>$value)
{
echo $key."=>".$value."<br>";
}
echo '<hr>';
//performing sort
arsort($people_ages);
echo "After Sort"."<br>";
//array after sorting
foreach($people_ages as $key=>$value)
{
echo $key."=>".$value."<br>";
}

Output:

PHP 中的排序

5. ksort()

This function performs sorting on the given array and arranges the keys of the array in ascending order. This example prints the array using a foreach loop and outputs the result as before sorting and after sorting.

Code:

//example to perform people_ages array
$people_ages = array('James' => 25, 'Rama' => 10, 'Mary' => 30, 'Alice' => 15, 'Radha' => 20);
// calculate length of array
$array_length = count($people_ages);
echo "Before Sort"."<br>";
//array before sorting
foreach($people_ages as $key=>$value)
{
echo $key."=>".$value."<br>";
}
echo '<hr>';
//performing sort
ksort($people_ages);
echo "After Sort"."<br>";
//array after sorting
foreach($people_ages as $key=>$value)
{
echo $key."=>".$value."<br>";
}

Output:

PHP 中的排序

6. krsort()

This function performs sorting on the given array and arranges the keys of the array in descending order. This example prints the array using a foreach loop and outputs the result as before sorting and after sorting.

Code:

//example to perform people_ages array
$people_ages = array('James' => 25, 'Rama' => 10, 'Mary' => 30, 'Alice' => 15, 'Radha' => 20);
// calculate length of array
$array_length = count($people_ages);
echo "Before Sort"."<br>";
//array before sorting
foreach($people_ages as $key=>$value)
{
echo $key."=>".$value."<br>";
}
echo '<hr>';
//performing sort
krsort($people_ages);
echo "After Sort"."<br>";
//array after sorting
foreach($people_ages as $key=>$value)
{
echo $key."=>".$value."<br>";
}

Output:

PHP 中的排序

7. natsort()

This function performs sorting on the given array and arranges the keys of the array in descending order. This example prints the array using a foreach loop and outputs the result as before sorting using assort() function and after sorting using natsort() function.

This function refreshes the output as the function randomizes the order of values in the given array. New numeric keys replace the keys mentioned in the array are assigned. For example, 10 is greater than 7 in a human being view, but according to the sorting algorithm 10 comes before 7.

We will use the natural flow of order.

Code:

<?php
$input = array("13 orange","14 Apple","15 3Banana","11 papaya","10 Grapes");;
$arr1 = $arr2 = $input;
echo "Before Sort"."<br>";
//array before sorting
foreach($input as $key=>$value)
{
echo $key."=>".$value."<br>";
}
echo '<hr>';
//performing sort
sort($arr1);
echo "Using asort function "."<br>";
//array before sorting
foreach($arr1 as $key=>$value)
{
echo $key."=>".$value."<br>";
}
echo '<hr>';
//performing sort
natsort($arr2);
echo "Using natsort function "."<br>";
foreach($arr2 as $key=>$value)
{
echo $key."=>".$value."<br>";
}
?>

Output :

PHP 中的排序

8. natcasesort()

This function works the same as natsort() but is case insensitive.

Code:

$input = array("13 orange","14 Apple","15 Banana","11 papaya","10 Grapes");;
$arr1 = $arr2 = $input;
echo "Before Sort"."<br>";
//array before sorting
foreach($input as $key=>$value)
{
echo $key."=>".$value."<br>";
}
echo '<hr>';
//performing sort
sort($arr1);
echo "Using asort function "."<br>";
//array before sorting
foreach($arr1 as $key=>$value)
{
echo $key."=>".$value."<br>";
}
echo '<hr>';
//performing sort
natcasesort($arr2);
echo "Using natcasesort function "."<br>";
foreach($arr2 as $key=>$value)
{
echo $key."=>".$value."<br>";
}

Output :

PHP 中的排序

9. usort()

This function performs sorting on the given array and arranges the values of the array in ascending order. This example prints the array using for loop and outputs the result.

In this program, the usort function takes two parameters: the input array and the other is the name of the function being called (here is compare).

This compare function is user-defined; also, the function is optional. This function returns 0 only if the condition in if block is satisfied, and else it will send -1 if the values compared is smaller than the other and 1 if the values compared is greater than the other.

Code:

function compare($x, $y) {
if($x == $y ){
return 0;
}
if($x < $y ){
return -1;
}
if($x > $y ){
return 1;
}
}
$numbers = array(10,4,5,3,20);
echo "Before Sort"."<br>";
//array after sorting
$array_length = count($numbers);
for($i=0;$i<$array_length;$i++)
{
echo $numbers[$i]."<br>";
}
echo '<hr>';
//performing sort
usort($numbers, "compare");
echo "After Sort"."<br>";
//array after sorting
$array_length = count($numbers);
for($i=0;$i<$array_length;$i++)
{
echo $numbers[$i]."<br>";
}

Output :

PHP 中的排序

10. uasort()

This function performs sorting on the given array and arranges the array’s values in ascending order using the compare function.

Code:

<?php
function compare($x, $y) {
if($x == $y ){
return 0;
}
if($x < $y ){
return -1;
}
if($x > $y ){
return 1;
}
}
echo '<hr>';
//performing sort
$input = array("num1"=>10,"num2"=>4,"num3"=>3,"num4"=>5, "num5"=>20);
uasort($input, "compare");
echo "After Sort"."<br>";
//array after sorting
$array_length = count($input);
foreach($input as $key=>$value)
{
echo $key."=>".$value."<br>";
}

Output:

PHP 中的排序

11. uksort()

This function performs sorting on the given array and arranges the array’s keys in ascending order using the compare function.

Code:

<?php
function compare($x, $y) {
if($x == $y ){
return 0;
}
if($x < $y ){
return -1;
}
if($x > $y ){
return 1;
}
}
echo '<hr>';
//performing sort
$input = array("num1"=>10,"num2"=>4,"num3"=>3,"num4"=>5, "num5"=>20);
uksort($input, "compare");
echo "After Sort"."<br>";
//array after sorting
$array_length = count($input);
foreach($input as $key=>$value)
{
echo $key."=>".$value."<br>";
}

Output :

PHP 中的排序

12. shuffle()

This function refreshes the output as the function randomizes the order of values in the given array. New numeric keys replace the keys mentioned in the array are assigned.

Code:

$input = array('a'=>"Guava",'e'=>"Apple",'b'=>"Orange",'c'=>"Papaya", 'd' => "Banana");
echo "Before Sort"."<br>";
//array before sorting
foreach($input as $key=>$value)
{
echo $key."=>".$value."<br>";
}
echo '<hr>';
shuffle($input);
echo 'You need to refresh to see the new shuffle everytime'.'<br>';
$array_length = count($input);
echo '<hr>';
//array after sorting
$array_length = count($input);
foreach($input as $key=>$value)
{
echo $key."=>".$value."<br>";
}

Output:

PHP 中的排序

Conclusion

In this article, most of the types of sorting are covered. The arrays are explained with examples. I hope you find it useful, informative and interesting.

以上是PHP 中的排序的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:PHP Form Validation下一篇:PHP usort()