Home  >  Article  >  Backend Development  >  How to sort one-dimensional array in php

How to sort one-dimensional array in php

PHPz
PHPzOriginal
2023-04-20 10:14:291401browse

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])

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);
?>

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
)

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])

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);
?>

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
)

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])

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);
?>

Output result:

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

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])

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);
?>

Output result:

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

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])

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);
?>

Output result:

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

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])

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);
?>

Output result:

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

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:
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