Home > Backend Development > PHP Tutorial > 'Natural' ordering of PHP arrays_PHP Tutorial

'Natural' ordering of PHP arrays_PHP Tutorial

WBOY
Release: 2016-07-14 10:10:21
Original
1315 people have browsed it

natsort
(PHP 4, PHP 5)

natsort — Sort an array using the "natural sorting" algorithm

Description
bool natsort (array &$array)
This function implements a sorting algorithm in the same way that people normally sort alphanumeric strings and maintains the original key/value association. This is called "natural sorting". The difference between this algorithm and the usual computer string sorting algorithm (used in sort()) is shown in the example below.

Parameters
array
input array.

Return value
Returns TRUE on success, or FALSE on failure.

Example #1 natsort() examples demonstrating basic usage

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

asort($array1);
echo "Standard sortingn";
print_r($array1);

natsort($array2);
echo "nNatural order sortingn";
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
)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477517.htmlTechArticlenatsort (PHP 4, PHP 5) natsort uses natural sorting algorithm to sort arrays. bool natsort (array $array) This function implements a method that people usually use to sort alphanumeric strings...
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