Array sort

PHP - Array sorting function

In this chapter, we will introduce the following PHP array sorting functions one by one:

  • sort() - Sort the array in ascending order

  • rsort() - Sort the array in descending order

  • asort() - Sort the array according to the value of the associative array Sort the array in ascending order

  • ksort() - Sort the array in ascending order according to the keys of the associative array

  • arsort() - Sort the array according to the keys of the associative array Sort the array in descending order

  • krsort() - Sort the array in descending order according to the key of the associated array


sort() - Sort the array in ascending order

The following example sorts the elements in the $cars array in ascending alphabetical order:

Example

The following example sorts the elements in the $numbers array in ascending numerical order:

Example

rsort() - Sort the array in descending order

The following example sorts the elements in the $cars array in descending alphabetical order:

Example

The following example sorts the elements in the $numbers array in descending numerical order:

Example

asort() - Sort the array in ascending order according to the value of the array

The following example sorts the association according to the value of the array Sort the array in ascending order:

Example

"35","Ben"=>"37","Joe"=>"43"); asort($age); ?>

ksort() - Sort the array in ascending order according to the key of the array

The following example is based on the array The key, sort the associative array in ascending order:

Example

"35","Ben"=>"37","Joe"=>"43"); ksort($age); ?>

arsort() - Sort the array in descending order according to the value of the array

The following example sorts an associative array in descending order based on the array's value:

Example

"35","Ben"=>"37","Joe"=>"43"); arsort($age); ?>

krsort() - Sorts the array in descending order based on the array's key

Below Example of sorts an associative array in descending order according to the key of the array:

Example

"35","Ben"=>"37","Joe"=>"43"); krsort($age); ?>
Continuing Learning
||
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!