Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use PHP array sorting

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use PHP array sorting, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

The sort () function is the ancestor of the sorting function in the array, and you must have found that all PHP array sorting functions are suffixed with sort. Function prototype: bool sort (array & array [, int sort_flags]) states that basically every function has an optional parameter, and sort is no exception. A habit indicated by this optional parameter.

There are several types to choose from:

◆ SORT_REGULAR-normal comparison unit (does not change type) / / sort by ASCII value (B is greater than a)

◆ SORT_NUMERIC-this parameter is commonly used as a number to compare / / a pair of integers and floating-point numbers

◆ SORT_STRING-units are compared as strings

◆ SORT_LOCALE_STRING-compare units as strings based on the current locale setting

Let's look at an example from the help manual:

The result of this PHP array sort is as follows:

Fruits [0] = apple fruits [1] = banana fruits [2] = lemon fruits [3] = orange

As you can see, it turned out to be a lemon with an index of 0, and then sorted it became apple. To keep the original key / value unchanged, you can replace sort () with asort (). The other two functions are the ascending order of keys (a comes before b). If you want to make it descend, just use rsort () instead, and the corresponding arsort () maintains the correlation of the original key / value.

The following is a brief introduction to the ksort () function

Analysis of the principle of PHP uploading multiple Files by APC method

Beginners must see the full disclosure of the progress of uploading files on PHP.

PHP upload file code of master's notes

Summary of PHP Array function from experience

In-depth study of PHP array function usage

This function exists mainly for associative arrays, and its "sister" function uksort () is compared using a custom function. Bottom line: ksort () for the association series, sorts the keys and retains the association of the key names to the data. Natural sorting of arrays, the sorting functions we know so far are sorted either by default or in a custom way. Then natsort () uses a humanized sorting method to sort the array. Not to mention here, examples can be seen in the manual, if you do not have a PHP help manual, you can browse the site online help manual to check

The shuffle () / function uses Shuffle () / / to sort an array randomly, which is a bit like drawing lots, but not much else. We'll see when we can use it. The use of array_rand () / / function array_rand () / / function is similar to shuffle (), but also randomly returns the elements of the array, the following call: array_rand (array [, int num_req]) / / optional parameter indicates the number you want to return. Thank you for reading this article carefully. I hope the article "how to use PHP array sorting" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report