PHP natsort array function

Last Updated: February 21, 2022

natsort – Sort an array using “natural order” algorithm

Syntax

natsort(array &$array): bool

Supports (PHP 4 , PHP 5, PHP 7, PHP 8)

Parameters

Parameter Description
array input array

Return Value

Always return true

Example

$array1 = $array2 = array('a', 'c', 'b', 'D', 'E', 'F');

sort($array1);
echo "Standard sorting\n";
print_r($array1);

natsort($array2);
echo "\nNatural order sorting\n";
print_r($array2);

Output

Standard sorting Array ( [0] => D [1] => E [2] => F [3] => a [4] => b [5] => c )

Natural order sorting Array ( [3] => D [4] => E [5] => F [0] => a [2] => b [1] => c )

Test