PHP array "natural" sorting _ PHP Tutorial

Source: Internet
Author: User
"Natural" sorting of PHP arrays. Natsort (PHP4, PHP5) natsort uses a natural sorting algorithm to sort arrays. boolnatsort (array $ array) this function provides a natsort that is generally used to sort alphanumeric strings.
(PHP 4, PHP 5)

Natsort-sort arrays using the "natural sorting" algorithm

Description
Bool natsort (array & $ array)
This function implements a sorting algorithm that is the same as the method that people usually sort character strings and maintain the Association of original keys/values. this is called "natural sorting ". The difference between this algorithm and the general computer string sorting algorithm (used for sort () is shown in the following example.

Parameters
Array
Input array.

Return value
Returns TRUE if the call succeeds, or FALSE if the call fails.

Example #1 natsort () examples demonstrating basic usage

$ Array1 = $ array2 = array ("img12.png", "img10.png", "img2.png", "img1.png ");

Asort ($ array1 );
Echo "Standard sorting \ n ";
Print_r ($ array1 );

Natsort ($ array2 );
Echo "\ nNatural order sorting \ n ";
Print_r ($ array2 );
?>
The above routine will output:

Standard sorting
Array
(
[3] => img1.png
[1] => img10.png
[0] => img12.png
[2] => img2.png
)

Natural order sorting
Array
(
[3] => img1.png
[2] => img2.png
[1] => img10.png
[0] => img12.png
)

Http://www.bkjia.com/PHPjc/477517.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477517.htmlTechArticlenatsort (PHP 4, PHP 5) natsort use natural sorting algorithms to sort arrays bool natsort (array $ array) this function is used to sort letters, numbers, and strings...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.