Showing posts with label PHP natcasesort. Show all posts
Showing posts with label PHP natcasesort. Show all posts

Thursday, 25 September 2014

natcasesort in PHP

PHP natcasesort() function is used to sort an array by using a "natural order" algorithm. Here values keep their original keys.
This function is case-insensitive, returns TRUE on success, or FALSE on failure.

Syntax:

natcasesort(array)
array : Required. Specifies the array to use.

Example:

<?php
$temp_files = array( "temp3.txt","Temp13.txt","temp22.txt","Temp18.txt","temp9.txt" );
natsort($temp_files);
echo "Natural order : ";
print_r($temp_files);
echo "<br />";
natcasesort ($temp_files);
echo "Natural order case insensitive :";
print_r($temp_files);
?>

O/P:

Natural order :
 Array (
         [0] => Temp13.txt 
         [1] => Temp18.txt 
         [2] => temp3.txt 
         [3] => temp9.txt 
         [4] => temp22.txt
       )
Natural order case insensitive :
Array (
        [0] => temp3.txt
        [1] => temp9.txt
        [2] => Temp13.txt 
        [3] => Temp18.txt 
        [4] => temp22.txt
       )