Home Backend Development PHP Problem How to sort one-dimensional array in php

How to sort one-dimensional array in php

Apr 20, 2023 am 10:14 AM

In PHP programming, we often need to sort arrays so that we can find and process data more easily. For one-dimensional arrays, you can sort by calling PHP's built-in functions. This article explains how to sort a one-dimensional array in PHP.

1. Sort() function

The sort() function is PHP’s built-in function for sorting arrays. The sort() function can sort an array in ascending or descending order. By default, the sort() function sorts in ascending order. Let’s take a look at the syntax of the sort function:

sort(array &$array [, int $sort_flags = SORT_REGULAR])
Copy after login

The parameter $array represents the array to be sorted, and the parameter $sort_flags represents the sorting rule. The default is SORT_REGULAR.

Example of using sort() function to sort a one-bit array in ascending order:

<?php
//定义一个一位数组
$array = array(5, 3, 1, 6, 9, 2);

//输出排序前的数组
echo "排序前:\n";
print_r($array);

//使用 sort() 函数进行排序
sort($array);

//输出排序后的数组
echo "排序后:\n";
print_r($array);
?>
Copy after login

Output result:

排序前:
Array
(
    [0] => 5
    [1] => 3
    [2] => 1
    [3] => 6
    [4] => 9
    [5] => 2
)
排序后:
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 5
    [4] => 6
    [5] => 9
)
Copy after login

2. rsort() function

The rsort() function is similar to the sort() function, except that the rsort() function sorts the array in descending order. Let’s take a look at the syntax of the rsort function:

rsort(array &$array [, int $sort_flags = SORT_REGULAR])
Copy after login

An example of using the rsort() function to sort a one-bit array in descending order:

<?php
//定义一个一位数组
$array = array(5, 3, 1, 6, 9, 2);

//输出排序前的数组
echo "排序前:\n";
print_r($array);

//使用 rsort() 函数进行排序
rsort($array);

//输出排序后的数组
echo "排序后:\n";
print_r($array);
?>
Copy after login

Output result:

排序前:
Array
(
    [0] => 5
    [1] => 3
    [2] => 1
    [3] => 6
    [4] => 9
    [5] => 2
)
排序后:
Array
(
    [0] => 9
    [1] => 6
    [2] => 5
    [3] => 3
    [4] => 2
    [5] => 1
)
Copy after login

3. asort() function

asort() function sorts an array in ascending order of the values ​​of key-value pairs. Let’s take a look at the syntax of the asort function:

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

An example of using the asort() function to sort a one-bit array in ascending order:

<?php
//定义一个一位数组
$array = array("b" => 3, "a" => 1, "c" => 2);

//输出排序前的数组
echo "排序前:\n";
print_r($array);

//使用 asort() 函数进行排序
asort($array);

//输出排序后的数组
echo "排序后:\n";
print_r($array);
?>
Copy after login

Output result:

排序前:
Array
(
    [b] => 3
    [a] => 1
    [c] => 2
)
排序后:
Array
(
    [a] => 1
    [c] => 2
    [b] => 3
)
Copy after login

In this In this example, we are sorting an array containing three string/value pairs. After sorting, the array will be sorted in ascending order of values.

4. arsort() function

arsort() function sorts the array in descending order by the value of the key-value pair. Let’s take a look at the syntax of the arsort function:

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

An example of using the arsort() function to sort a one-bit array in descending order:

<?php
//定义一个一位数组
$array = array("b" => 3, "a" => 1, "c" => 2);

//输出排序前的数组
echo "排序前:\n";
print_r($array);

//使用 arsort() 函数进行排序
arsort($array);

//输出排序后的数组
echo "排序后:\n";
print_r($array);
?>
Copy after login

Output result:

排序前:
Array
(
    [b] => 3
    [a] => 1
    [c] => 2
)
排序后:
Array
(
    [b] => 3
    [c] => 2
    [a] => 1
)
Copy after login

The above Example shows how to sort an array containing key-value pairs. After sorting, it will be sorted by value in descending order.

5. ksort() function

ksort() function sorts the array in ascending order by key name. Let’s take a look at the syntax of the ksort function:

ksort(array &$array [, int $sort_flags = SORT_REGULAR])
Copy after login

An example of using the ksort() function to sort a one-bit array in ascending order:

<?php
//定义一个一位数组
$array = array("b" => 3, "a" => 1, "c" => 2);

//输出排序前的数组
echo "排序前:\n";
print_r($array);

//使用 ksort() 函数进行排序
ksort($array);

//输出排序后的数组
echo "排序后:\n";
print_r($array);
?>
Copy after login

Output result:

排序前:
Array
(
    [b] => 3
    [a] => 1
    [c] => 2
)
排序后:
Array
(
    [a] => 1
    [b] => 3
    [c] => 2
)
Copy after login

This example Shows how to sort the keys of an array. The sort results will be sorted alphabetically by key in ascending order.

6. krsort() function

krsort() function sorts the array in descending order by key name. Let’s take a look at the syntax of the krsort function:

krsort(array &$array [, int $sort_flags = SORT_REGULAR])
Copy after login

An example of using the krsort() function to sort a one-bit array in descending order:

<?php
//定义一个一位数组
$array = array("b" => 3, "a" => 1, "c" => 2);

//输出排序前的数组
echo "排序前:\n";
print_r($array);

//使用 krsort() 函数进行排序
krsort($array);

//输出排序后的数组
echo "排序后:\n";
print_r($array);
?>
Copy after login

Output result:

排序前:
Array
(
    [b] => 3
    [a] => 1
    [c] => 2
)
排序后:
Array
(
    [c] => 2
    [b] => 3
    [a] => 1
)
Copy after login

This example Shows how to sort the keys of an array. The sort results will be sorted alphabetically by key in descending order.

Summary:

The above is how PHP sorts a one-bit array. PHP's built-in sort(), rsort(), asort(), arsort(), ksort() and krsort() functions can easily sort arrays, and we can choose the function that suits us for sorting according to our needs. Speed ​​up program execution efficiency and improve program performance.

The above is the detailed content of How to sort one-dimensional array 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)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1238
24