Home > Backend Development > PHP Tutorial > php array sorting function

php array sorting function

angryTom
Release: 2023-04-07 16:20:01
forward
2331 people have browsed it

This article introduces 6 PHP array sorting functions, I hope it will be helpful to you.

  • sort() – Sort the array in ascending order (passing a reference will modify the original array)

  • rsort() – Sort the array in descending order Arrange (passing by reference will modify the original array)

  • asort() – Sort the array in ascending order based on the value of the associated array

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

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

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

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/12
 * Time: 9:46
 */
$arr = [&#39;Tom&#39;=>&#39;11&#39;,&#39;Jerry&#39;=>20,&#39;heros&#39;=>[&#39;caption&#39;,&#39;superman&#39;]];
print_r($arr);
sort($arr);
print_r($arr);
rsort($arr);
print_r($arr);
asort($arr);
print_r($arr);
ksort($arr);
print_r($arr);
arsort($arr);
print_r($arr);
krsort($arr);
print_r($arr);
Copy after login

The execution results are as follows

Array
(
    [Tom] => 11
    [Jerry] => 20
    [heros] => Array
        (
            [0] => caption
            [1] => superman
        )
)
Array
(
    [0] => 11
    [1] => 20
    [2] => Array
        (
            [0] => caption
            [1] => superman
        )
)
Array
(
    [0] => Array
        (
            [0] => caption
            [1] => superman
        )
    [1] => 20
    [2] => 11
)
Array
(
    [2] => 11
    [1] => 20
    [0] => Array
        (
            [0] => caption
            [1] => superman
        )
)
Array
(
    [0] => Array
        (
            [0] => caption
            [1] => superman
        )
    [1] => 20
    [2] => 11
)
Copy after login

Things to note The processing of arrays in PHP is all reference processing, which means that the original array will be modified. Therefore, if you do not want to modify the original array, it is recommended to create a new array machine for sorting operations.

Of course, generally when we want to sort, we still want the original array to be modified into a sorted array.

For more PHP related knowledge, please visit PHP Chinese website !

The above is the detailed content of php array sorting function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:www.80shihua.com
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