In PHP, array is a very commonly used data type, and in actual development, we often need to sort arrays. PHP provides a variety of sorting algorithms and functions, which can make sorting operations very convenient and efficient. This article will introduce the specific implementation method of multi-array sorting in PHP.
I. One-dimensional array sorting
PHP provides a variety of sorting algorithms and functions, which can make sorting operations convenient and efficient. Among them, the most commonly used functions are sort(), rsort(), asort(), arsort(), ksort(), krsort(), which are used to sort arrays of different types.
The sort() and rsort() functions are used to sort a one-dimensional array in ascending order (from small to large) and descending order (from Largest to Smallest) sorted. The sort() function sorts the elements in ascending order, and the rsort() function sorts the elements in descending order.
The following is an example of using the sort() function to sort a one-dimensional array in ascending order:
<?php $fruits = array("lemon", "orange", "banana", "apple"); sort($fruits); print_r($fruits); ?>
Output result:
Array ( [0] => apple [1] => banana [2] => lemon [3] => orange )
The following is an example of using the rsort() function to sort a one-dimensional array in ascending order: Example of one-dimensional array sorting in descending order:
<?php $fruits = array("lemon", "orange", "banana", "apple"); rsort($fruits); print_r($fruits); ?>
Output results:
Array ( [0] => orange [1] => lemon [2] => banana [3] => apple )
asort() and arsort () function is used to sort a one-dimensional array in ascending order (from small to large) and descending order (from large to small). Unlike sort() and rsort(), the asort() and arsort() functions keep the index relationship of the array unchanged.
The following is an example of using the asort() function to sort a one-dimensional array in ascending order:
<?php $age = array("Peter"=>35, "Ben"=>37, "Joe"=>43); asort($age); print_r($age); ?>
Output result:
Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
The following is an example of using the asort() function to sort a one-dimensional array in ascending order: Example of one-dimensional array sorting in descending order:
<?php $age = array("Peter"=>35, "Ben"=>37, "Joe"=>43); arsort($age); print_r($age); ?>
Output results:
Array ( [Joe] => 43 [Ben] => 37 [Peter] => 35 )
ksort() and krsort The () function is used to sort a one-dimensional array in ascending order (from small to large) or descending order (from large to small) by key. The ksort() function sorts by key value in ascending order, while the krsort() function sorts by key value in descending order.
The following is an example of using the ksort() function to sort a one-dimensional array in ascending order by key:
<?php $age = array("Peter"=>35, "Ben"=>37, "Joe"=>43); ksort($age); print_r($age); ?>
Output result:
Array ( [Ben] => 37 [Joe] => 43 [Peter] => 35 )
The following is an example of using krsort() Example of the function sorting a one-dimensional array in descending order by key:
<?php $age = array("Peter"=>35, "Ben"=>37, "Joe"=>43); krsort($age); print_r($age); ?>
Output result:
Array ( [Peter] => 35 [Joe] => 43 [Ben] => 37 )
II. Multi-dimensional array sorting
For multi-dimensional array sorting, if we sort according to the To sort the elements of a one-dimensional array, you can use the four functions sort(), rsort(), asort(), and arsort(). But if we need to sort by other keys of the multidimensional array, we need to use the usort() function.
usort() function allows us to customize the sorting function to meet our various sorting needs. When we customize the sorting function, we receive two parameters, which refer to the two elements to be compared. When the comparison result of these two elements is less than 0, put $elem1 before $elem2, otherwise put $elem1 after $elem2.
The following is an example of using the usort() function to sort a multi-dimensional array:
<?php $cars = array( array("Volvo",22,18), array("BMW",15,13), array("Saab",5,2), array("Land Rover",17,15) ); function my_sort($a,$b) { if ($a[0] == $b[0]) return 0; return ($a[0] < $b[0]) ? -1 : 1; } usort($cars, "my_sort"); foreach ($cars as $car) { echo $car[0] . " " . $car[1] . " " . $car[2] . "<br>"; } ?>
Output result:
BMW 15 13 Land Rover 17 15 Saab 5 2 Volvo 22 18
In the above example, we define a my_sort () function, which is used to compare the first element of the array. Then call the usort() function, and pass the multidimensional array to be sorted and the custom sorting function name to the usort() function. After the sorting is completed, we use the foreach statement to traverse the sorted results.
To sum up, PHP provides a variety of sorting algorithms and functions to meet our various sorting needs. Whether it is a one-dimensional array or a multi-dimensional array, PHP provides different sorting functions to operate, allowing us to sort the array easily and conveniently.
The above is the detailed content of Let's talk about the specific implementation method of multi-array sorting in PHP. For more information, please follow other related articles on the PHP Chinese website!