In this article, you will learn how to sort an array in the natural order. In Natural Order, the number 2 is less than the number 10. But in computer sorting, the number 10 is less than 2 because the first digit in 10 is 1 which is less than 2.
The natsort() function rearranges the elements of an array in natural order using natural order algorithm.
This function is case-sensitive.
What is the syntax of the natsort() function in PHP?
natsort(array)
Parameter | Details |
---|---|
array | The array to sort the elements of – Required |
Example of natsort function
Example 1. Arrange the elements of the array in the natural order.
<?php
arr = ["day1","day2","day3","day4","day5"];
natsort($arr);
echo "Natural order:";
print_r($arr);
?>
Example 2. Case-insensitive behavior of the function.
<?php
arr = ["day1","day2","day3","day4","day5"];
natcasesort($arr);
echo "Natural order: ";
print_r($arr);
?>