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 natcasesort () function rearranges the elements of an array in natural order using natural order algorithm.
This function is case-insensitive.
What is the syntax of the natcasesort() function in PHP?
natcasesort(array)
Parameter | Details |
---|---|
array | The array to sort the elements of – Required |
Example of natcasesort 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 natcasesort function.
<?php
arr = ["day1","day2","day3","day4","day5"];
natcasesort($arr);
echo "Natural order: ";
print_r($arr);
?>