Home > Backend Development > PHP Problem > How many types of php array sorting are there?

How many types of php array sorting are there?

青灯夜游
Release: 2023-03-16 10:16:01
Original
8989 people have browsed it

There are 12 types of php array sorting: 1. Use sort() to sort the array in ascending order; 2. Use rsort() to sort the array in descending order; 3. Use asort() to sort the array in ascending order based on the value of the associated array. Arrange; 4. Use ksort() to sort in ascending order according to the keys of the associative array; 5. Use krsort() to sort in descending order, etc.

How many types of php array sorting are there?

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

In the process of using PHP arrays, we have Sometimes you need to sort a PHP array.

In PHP, the elements in an array can be arranged in descending or ascending order alphabetically or numerically. The following is PHP's built-in array sorting function:

##FunctionDescriptionarray_multisort() Sort multiple arrays or multidimensional arrays. arsort() Sort the associative array in descending order by key value. asort() Sort the associative array in ascending order by key value. krsort() Sort the associative array in descending order by key name. ksort() Sort the associative array in ascending order by key name. natcasesort() Use the "natural sorting" algorithm to sort the array in a case-insensitive manner. natsort() Sort the array using the "natural sorting" algorithm. rsort() Sort the numeric array in descending order. sort() Sort the numeric array in ascending order. uasort()Use a user-defined comparison function to sort the key values ​​in the array. uksort() Use a user-defined comparison function to sort the key names in the array. usort() Sort the array using a user-defined comparison function.
Among them, there are six commonly used ones:

  • sort() - Sort the array in ascending order

  • rsort() - Sort an array in descending order

  • asort() - Sort an array in ascending order based on the values ​​of an associative array

  • ksort() - Sort an array in ascending order based on the keys of an associative array

  • arsort() - Sort an array in descending order based on the values ​​of an associative array

  • krsort() - Sort the array in descending order based on the keys of the associative array

#sort() - Sort the array in ascending order

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

<?php
$cars=array("Volvo","BMW","Toyota");
var_dump($cars);
sort($cars);
var_dump($cars);
?>
Copy after login

How many types of php array sorting are there?

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

<?php
$numbers=array(4,6,2,22,11);
var_dump($numbers);
sort($numbers);
var_dump($numbers);
?>
Copy after login

How many types of php array sorting are there?

rsort() - Sort the array in descending order

The following example sorts the elements in the $cars array according to Alphabetical descending order:

<?php
$cars=array("Volvo","BMW","Toyota");
var_dump($cars);
rsort($cars);
var_dump($cars);
?>
Copy after login

How many types of php array sorting are there?

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

<?php
$numbers=array(4,6,2,22,11);
var_dump($numbers);
rsort($numbers);
var_dump($numbers);
?>
Copy after login

2-How many types of php array sorting are there?

asort() - Sort the array in ascending order based on the value of the array

The following example sorts the associative array in ascending order based on the value of the array:

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
var_dump($age);
asort($age);
var_dump($age);
?>
Copy after login

How many types of php array sorting are there?

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

The following example sorts the associative array in ascending order according to the key of the array:

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
var_dump($age);
ksort($age);
var_dump($age);
?>
Copy after login

How many types of php array sorting are there?

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

The following example sorts the array according to the value of the array Sort the associative array in descending order:

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
var_dump($age);
arsort($age);
var_dump($age);
?>
Copy after login

How many types of php array sorting are there?

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

The following The example sorts the associated array in descending order according to the key of the array:

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
var_dump($age);
krsort($age);
var_dump($age);
?>
Copy after login

How many types of php array sorting are there?

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How many types of php array sorting are there?. 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