Home Daily Programming PHP Knowledge How to sort array values ​​in ascending and descending order in PHP

How to sort array values ​​in ascending and descending order in PHP

Jan 16, 2019 pm 02:32 PM

PHP sorts array values ​​in ascending or descending order, which is also one of the common basic PHP interview questions. We can achieve this through the two functions asort() and arsort() in PHP.

How to sort array values ​​in ascending and descending order in PHP

Below we will use a simple code example to introduce to you the method of PHP sorting in ascending and descending order according to array values.

1. PHP sorts the array in ascending order

The code example is as follows:

<?php
//按照数组的值来进行数组的排序
//按值升序排序
$arr1 = array("西门"=> "29","韦小宝"=>"25","灭绝"=>"18","朱老师"=>"32");
asort($arr1);
foreach ($arr1 as $k => $v){
    echo "年龄:".$k."是:".$v."
    ";
}
echo "<br>";
Copy after login

The result of ascending sorting is as shown below:

How to sort array values ​​in ascending and descending order in PHP

2. PHP sorts the array value in descending order

The code example is as follows:

<?php
//按照数组的值来进行数组的排序
//按值降序排序
$arr1 = array("西门"=> "29","韦小宝"=>"25","灭绝"=>"18","朱老师"=>"32");
arsort($arr1);
foreach ($arr1 as $k => $v){
    echo "年龄:".$k."是:".$v."
    ";
}
Copy after login

The descending sorting result is as shown below:

How to sort array values ​​in ascending and descending order in PHP

Function introduction:

asort: Sort the array and maintain the index relationship

asort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) : bool
Copy after login

This function sorts the array, and the index of the array remains associated with the unit. Mainly used for sorting associative arrays where the order of cells is important.

The parameter array represents the input array. sort_flags The optional parameter sort_flags can be used to change the sorting behavior.

Return value, TRUE on success, or FALSE on failure.

arsort: Sort the array in reverse order and maintain the index relationship

arsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) : bool
Copy after login

This function sorts the array, and the index of the array remains the same as the unit's association. Mainly used for sorting associative arrays where the order of cells is important.

The parameter array represents the input array. sort_flags The optional parameter sort_flags can be used to change the sorting behavior.

Return value, TRUE on success, or FALSE on failure.

This article is about PHP's method of sorting array values ​​in ascending and descending order. It is very simple. I hope it will be helpful to friends in need!

The above is the detailed content of How to sort array values ​​in ascending and descending order in PHP. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)