Jump to: navigation, search

Php natcasesort

From w3cyberlearnings

Contents

PHP function natcasesort

This function sorts array by using the natural order algorithm, and this sorting function is case-insensitive.
Natural order algorithm works this way: 1 is before 2, 500 is before 501.

Syntax natcasesort

  • array: array input
natcasesort(array);

Note

For case sensitive natural order sorting uses natsort() function.

Example 1

<?php

$tfile = array(
	 'file01',
	 'file02',
	 'file30',
	 'file1',
	 'file5',
	 'File51',
	 'File30',
	 'file06'
);

natcasesort($tfile);
print_r($tfile);
?>

Output


Array
(
    [0] => file01
    [1] => file02
    [7] => file06
    [3] => file1
    [4] => file5
    [6] => File30
    [2] => file30
    [5] => File51
)

Example 2

<?php

$name_s = array(
	 'bob' => 'R302',
	 'janny' => 'A304',
	 'mark' => 'A303',
	 'john'=>'a305',
	 'mark3'=>'a302'
);
natcasesort($name_s);
print_r($name_s);
?>

Output

Array
(
    [mark3] => a302
    [mark] => A303
    [janny] => A304
    [john] => a305
    [bob] => R302
)

Related Links


Navigation
Web
SQL
MISC
References