Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Array Functions : natcasesort

natcasesort

Sort an array using a case insensitive "natural order" algorithm (PHP 4, PHP 5)
bool natcasesort ( array &array )

Example 315. natcasesort() example

<?php
$array1
= $array2 = array('IMG0.png', 'img12.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png');

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

natcasesort($array2);
echo
"\nNatural order sorting (case-insensitive)\n";
print_r($array2);
?>

The above example will output:

Standard sorting
Array
(
   [0] => IMG0.png
   [1] => IMG3.png
   [2] => img1.png
   [3] => img10.png
   [4] => img12.png
   [5] => img2.png
)

Natural order sorting (case-insensitive)
Array
(
   [0] => IMG0.png
   [4] => img1.png
   [3] => img2.png
   [5] => IMG3.png
   [2] => img10.png
   [1] => img12.png
)

For more information see: Martin Pool's » Natural Order String Comparison page.


Code Examples / Notes » natcasesort

vbalexdosman

Ulli at Stemmeler dot net:  I remade your function -- it's a little more compact now -- Enjoy...
function ignorecasesort(&$array) {
 /*Make each element it's lowercase self plus itself*/
 /*(e.g. "MyWebSite" would become "mywebsiteMyWebSite"*/
 for ($i = 0; $i < sizeof($array); $array[$i] = strtolower($array[$i]).$array[$i], $i++);
 /*Sort it -- only the lowercase versions will be used*/
 sort($array);
 /*Take each array element, cut it in half, and add the latter half to a new array*/
 /*(e.g. "mywebsiteMyWebSite" would become "MyWebSite")*/
 for ($i = 0; $i < sizeof($array); $i++) {
   $this = $array[$i];
   $array[$i] = substr($this, (strlen($this)/2), strlen($this));
 }
}


dslicer

Something that should probably be documented is the fact that both natsort and natcasesort maintain the key-value associations of the array. If you natsort a numerically indexed array, a for loop will not produce the sorted order; a foreach loop, however, will produce the sorted order, but the indices won't be in numeric order. If you want natsort and natcasesort to break the key-value associations, just use array_values on the sorted array, like so:
natcasesort($arr);
$arr = array_values($arr);


ulli

natcasesort didn't work first time I needed something like this.
Not on my local server, not on my server on the web.
I needed an array sorted ignoring upper and lower cases. In the end lower case array-members stayed at the end of the array.
I replaced it with this function:
---------
function ignorecasesort(&$array) {
$separator="|<>|";
for($i=0;$i<sizeof($array);$i++) { $array[$i]=strtolower($array[$i]).$separator.$array[$i]; }
sort($array);
for($i=0;$i<sizeof($array);$i++) { $this=$array[$i]; $this=explode($separator,$this); $array[$i]=$this[1]; }
}
---------


php

hi all,
this is my first post at php.net first of all thank you for this huge site :)
ok i found it usefull to post this for others. in the german language we have words like 'ä ö ü' its called "umlaute" and it was a problem to me to "naturally" sort an huge array correctly so i coded this small block to get me helped, if anyone has an better idea please post and let us know as i am not a real crack :)
1. change each "umlaut" (äöüß) into its "nearest" equivalent
2. natcasesorting it naturally
3. re-assiging the correct sorted array with the original-words again while keeping track of the KEY...
example-array:
$aSupplier = array(3 => "MAN",2 => "Atlas",16 => "Chevrolet",17 => "Chrysler",19 => "Citroen",24 => "DAF",25 => "Daihatsu",27 => "Daewoo",28 => "Demag",30 => "Dodge",36 => "Schierling",208 => "HIAB",38 => "Hüffermann",39 => "Gergen",40 => "Kubato",41 => "Faun",43 => "Kleindienst",44 => "Swing",45 => "Neuhaus",46 => "Unimog",47 => "Meiller",48 => "Pfau-Johnston",49 => "Geesink",50 => "Schörling",51 => "Demag-Witting",53 => "Helmers",54 => "Ellermann",55 => "Jacobsen",56 => "Biki",57 => "Hansa",58 => "Kramer",59 => "Schmitz",60 => "Toro",61 => "Iseki",62 => "Haller",63 => "Kuka",64 => "Brock",65 => "Ambross",66 => "Sobernheimer",67 => "Pietsch",68 => "Küpper",69 => "Weisser",71 => "Wackenhut");
foreach ( $aSupplier as $key => $value )
{
$aSupplier2[$key] = strtr($aSupplier[$key], "ÄÖÜäöüß", "AOUaous");
}
natcasesort($aSupplier2);
foreach ( $aSupplier2 as $key => $value )
{
 if ( $aSupplier2[$key] != "" )
$aSupplier3[$key] = $aSupplier[$key];
}
echo "<pre>";
print_r($aSupplier3);
echo "</pre>";
greetz
tim


tmiller25

add this loop to the function above if you want items which have the same first characters to be listed in a way that the shorter string comes first.
--------------------
 /* short before longer (e.g. 'abc' should come before 'abcd') */
 for($i=count($array)-1;$i>0;$i--) {
   $str_a = $array[$i  ];
   $str_b = $array[$i-1];
   $cmp_a = strtolower(substr($str_a,0,strlen($str_a)));
   $cmp_b = strtolower(substr($str_b,0,strlen($str_a)));
   if ($cmp_a==$cmp_b && strlen($str_a)<strlen($str_b)) {
     $array[$i]=$str_b; $array[$i-1]=$str_a; $i+=2;
   }
 }
--------------------


Change Language


Follow Navioo On Twitter
array_change_key_case
array_chunk
array_combine
array_count_values
array_diff_assoc
array_diff_key
array_diff_uassoc
array_diff_ukey
array_diff
array_fill_keys
array_fill
array_filter
array_flip
array_intersect_assoc
array_intersect_key
array_intersect_uassoc
array_intersect_ukey
array_intersect
array_key_exists
array_keys
array_map
array_merge_recursive
array_merge
array_multisort
array_pad
array_pop
array_product
array_push
array_rand
array_reduce
array_reverse
array_search
array_shift
array_slice
array_splice
array_sum
array_udiff_assoc
array_udiff_uassoc
array_udiff
array_uintersect_assoc
array_uintersect_uassoc
array_uintersect
array_unique
array_unshift
array_values
array_walk_recursive
array_walk
array
arsort
asort
compact
count
current
each
end
extract
in_array
key
krsort
ksort
list
natcasesort
natsort
next
pos
prev
range
reset
rsort
shuffle
sizeof
sort
uasort
uksort
usort
eXTReMe Tracker