In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to sort the php array". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to sort the php array" can help you solve the problem.
The php array can be sorted. Php has a variety of array sorting functions: 1, sort (), can be sorted in ascending order, syntax "sort (array)"; 2, rsort (), can be sorted in descending order, syntax "rsort (array)"; 3, shuffle (), can be sorted randomly, syntax "array".
Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
The php array can be sorted.
Php provides a variety of built-in functions for different array sorting requirements (key values, key names, ascending, descending, and so on).
1. Sort the array in ascending order
There are three commonly used array ascending sort functions in PHP:
Sort (): sort array elements in ascending order
Asort (): sorts the array in ascending order according to the key value of the associative array
Ksort (): sorts 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).
Output:
The sort () function takes two arguments: $array (required) and $sortingtype (omitted).
Where the $sortingtype parameter is used to define the mode of function sorting, specifying how the elements / items of the array are compared, and the default value is "SORT_REGULAR".
The $sortingtype parameter can be set to the following values:
0 = SORT_REGULAR: normal comparison of array elements without changing their type (default)
1 = SORT_NUMERIC: treats array elements as numbers
2 = SORT_STRING: treats array elements as strings
3 = SORT_LOCALE_STRING: compare array elements as strings based on the current locale (which can be changed through setlocale ()).
4 = SORT_NATURAL: similar to natsort (), which sorts strings in "natural order" for each array element, is new in PHP5.4.0.
5 = SORT_FLAG_CASE: can be combined with SORT_STRING or SORT_NATURAL (OR bit operation), sorting strings without case sensitivity.
Output result:
The sort () function does not maintain the index relationship, but deletes the original key name from the array and assigns it a new numeric key name.
Output:
The sort () function can be used for arrays (numeric arrays) where the relationship between values and indexes is less important, 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 sorts in ascending order based on the key values of the associative array and does not modify the key names in the original array.
Header ("Content-type:text/html;charset=utf-8"); $age = array ("Zhang San" = > 30, "Li Si" = > 23, "Wang Wu" = > 15, "Li Hua" = > 12, "Nana" = > 26, "Xiao Hong" = > 16); asort ($age); var_dump ($age);? >
Output:
The asort () function also has two parameters, which are the same as the sort () function, which can be used for reference.
3. Use the ksort () function
The ksort () function sorts in ascending order based on the key name of the associative array, and does not modify the key name in the original array.
Output:
Output:
The ksort () function also has two parameters, which are the same as the sort () function, which can be used for reference.
2. Sort the array in descending order
There are three commonly used array descending sort functions in PHP:
Rsort (): sort array elements in descending order
Arsort (): sorts the array in descending order according to the key value of the associative array
Krsort (): sorts the array in descending order according to the key name of the associative array
1. Use the rsort () function
The rsort () function is used to sort array elements in descending order, that is, from large to small, from high to low.
Let's take a look at the code example:
Output result:
The rsort () function takes two arguments: $array (required) and $sortingtype (omitted). The $sortingtype parameter is used to indicate the sort mode. For specific values, please refer to the sort () function in the previous article. The following arsort () and krsort () also refer to the sort () function.
Output result:
Rsort (), like the sort () function, does not maintain an index relationship, but removes the original key name from the array and reassigns it to it; therefore, it is not suitable for arrays that pay attention to the relationship between values and indexes.
2. Use the arsort () function
The arsort () function sorts in descending order based on the key values of the associative array and does not modify the key names in the original array.
Output result:
3. Use the krsort () function
The krsort () function sorts in descending order based on the key name of the associative array, and does not change the key name in the original array.
Output result:
3. Random sorting
In PHP, you can use the shuffle () function to randomly sort and disrupt array elements.
The shuffle () function rearranges the elements in the array in random order.
Output result:
Refresh it, and the result changes:
Note: the shuffle () function will not only randomly disrupt the array, but also delete the original key name in the array and assign it a new numeric key name (that is, it will not maintain the index relationship).
That's all for "how to sort php arrays". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.