Are php arrays in order?

小老鼠
Release: 2023-06-30 15:12:09
Original
1086 people have browsed it

php arrays have order. The sorting methods are: 1. Use the sort() function to sort in ascending order, the syntax is "sort(array)"; 2. Use the rsort() function to sort in descending order, the syntax is "rsort(array)"; 3. Use The shuffle() function can perform random sorting, and the syntax is "shuffle(array)".

Are php arrays in order?

The operating environment of this tutorial: Windows 10 system, PHP8.1.3 version, Dell G3 computer.

php provides a variety of built-in functions for different array sorting needs (key value, key name, ascending order, descending order, etc.).

1. Sort the array in ascending order

There are three commonly used ascending array sorting functions in PHP:

sort(): Sort the array elements Sort in ascending order

asort(): Sort the array in ascending order according to the key value of the associated array

ksort(): Sort the array in ascending order based on the key name of the associated array

1. Use the sort() function

The sort() function sorts the array elements in ascending order (from small to large, from low to high).

<?php
header("Content-type:text/html;charset=utf-8");
$arr1 = array(10, 23, 5, 12, 84, 16);
sort($arr1);
var_dump($arr1);
 
$arr2= array("香蕉","苹果","梨子","橙子","橘子","榴莲");
sort($arr2);
var_dump($arr2);
?>
Copy after login

Output:

Are php arrays in order?

The sort() function has two parameters: $array (required) and $sortingtype (can be omitted).

Among them, the $sortingtype parameter is used to define the function sorting mode and specify how to compare the elements/items of the array. The default value is "SORT_REGULAR". The

$sortingtype parameter can be set to the following values:

0 = SORT_REGULAR: Compare array elements normally without changing their type (default value);

1 = SORT_NUMERIC: Treat array elements as numbers;

2 = SORT_STRING: Treat array elements as strings;

3 = SORT_LOCALE_STRING: Based on the current locale (available via setlocale() changes) to compare array elements as strings.

4 = SORT_NATURAL: Similar to natsort(), it sorts strings in "natural order" for each array element. It is new in PHP5.4.0.

5 = SORT_FLAG_CASE: Ability to combine with SORT_STRING or SORT_NATURAL (OR bitwise operation), case-insensitive sorting of strings.

<?php
header("Content-type:text/html;charset=utf-8");
$arr1 = array(10, 23, 5, 12, 84, 16);
sort($arr1,2);
var_dump($arr1);
 
$arr2= array("香蕉","苹果","梨子","橙子","橘子","榴莲");
sort($arr2,1);
var_dump($arr2);
?>
Copy after login

Output result:

Are php arrays in order?

The sort() function will not maintain the index relationship, but will delete the original key name in the array and assign it a new one Numeric key name.

<?php
header("Content-type:text/html;charset=utf-8");
$age = array("张三"=>30,"李四"=>23,"王五"=>15,"李华"=>12,"娜娜"=>26,"小红"=>16);
sort($age);
var_dump($age);
?>
Copy after login

Output:

Are php arrays in order?

The sort() function can be used for arrays (numeric arrays) where the relationship between values ​​and indexes is not so important, but the value and Arrays of relationships between indexes are not suitable. At this point, you need to use the asort() and ksort() functions.

2. Use the asort() function

The asort() function will sort the associative array in ascending order based on the key values ​​and will not modify the key names in the original array. .

header("Content-type:text/html;charset=utf-8");
$age = array("张三"=>30,"李四"=>23,"王五"=>15,"李华"=>12,"娜娜"=>26,"小红"=>16);
asort($age);
var_dump($age);
?>
Copy after login

Output:

Are php arrays in order?

The asort() function also has two parameters. The parameter values ​​are the same as the sort() function, you can refer to them.

3. Use the ksort() function

The ksort() function will sort the associative array in ascending order according to the key names, and will not modify the key names in the original array. .

<?php
header("Content-type:text/html;charset=utf-8");
$age = array("张三"=>30,"李四"=>23,"王五"=>15,"李华"=>12,"娜娜"=>26,"小红"=>16);
ksort($age);
var_dump($age);
?>
Copy after login

Output:

Are php arrays in order?

<?php
header("Content-type:text/html;charset=utf-8");
$arr= array("l"=>"lemon", "o"=>"orange", "b"=>"banana", "a"=>"apple");
ksort($arr);
var_dump($arr);
?>
Copy after login

Output:

Are php arrays in order?

ksort() function also has two parameters , the parameter values ​​are the same as the sort() function, you can refer to them.

The above is the detailed content of Are php arrays in order?. 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