How to sort one-dimensional array in ascending order in php (without removing duplicates)

青灯夜游
Release: 2023-03-16 19:54:02
Original
2047 people have browsed it

Three methods of ascending sorting: 1. Use the sort() function to sort the array elements in ascending order, the syntax is "sort($arr, sort mode);"; 2. Use the asort() function, It can be sorted in ascending order according to the key value of the associative array, the syntax is "asort($arr, sorting mode)"; 3. Using the ksort() function, it can be sorted in ascending order according to the key name of the associative array, the syntax is "ksort($arr, sorting mode)" model)".

How to sort one-dimensional array in ascending order in php (without removing duplicates)

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer

In PHP, one-to-one without deduplication Three common methods for ascending sorting of dimensional arrays:

  • sort(): Sort array elements in ascending order

  • asort(): According to Sort the array in ascending order according to the key value of the associative array

  • ksort(): Sort the array in ascending order according to the key name of the associative array

1. Use the sort() function

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

Copy after login

Output:

How to sort one-dimensional array in ascending order in php (without removing duplicates)

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: Compare array elements as strings based on the current locale (can be changed via setlocale()).

  • 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: Can be combined with SORT_STRING or SORT_NATURAL (OR bitwise operation), case-insensitive sorting string.

Copy after login
Output result:


How to sort one-dimensional array in ascending order in php (without removing duplicates)

sort() function will not maintain the index relationship and will delete the array to the original key name and assign it a new numeric key name.

30,"李四"=>23,"王五"=>15,"李华"=>12,"娜娜"=>26,"小红"=>16); sort($age); var_dump($age); ?>
Copy after login

Output:


How to sort one-dimensional array in ascending order in php (without removing duplicates)

The sort() function can be used for arrays where the relationship between values and indexes is not so important (numeric arrays), but Arrays that focus on the relationship between values and 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:


How to sort one-dimensional array in ascending order in php (without removing duplicates)

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

3. Use the ksort() function

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

30,"李四"=>23,"王五"=>15,"李华"=>12,"娜娜"=>26,"小红"=>16); ksort($age); var_dump($age); ?>
Copy after login

Output:


##

"lemon", "o"=>"orange", "b"=>"banana", "a"=>"apple"); ksort($arr); var_dump($arr); ?>
Copy after login
How to sort one-dimensional array in ascending order in php (without removing duplicates)Output:


How to sort one-dimensional array in ascending order in php (without removing duplicates)## The #ksort() function also has two parameters. The parameter values are the same as the sort() function. You can refer to them.

Recommended learning: "

PHP Video Tutorial
"

The above is the detailed content of How to sort one-dimensional array in ascending order in php (without removing duplicates). 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!