Sort an array using a case insensitive "natural order" algorithm with ArrayObject::natcasesort in PHP

Description

The following code shows how to sort an array using a case insensitive "natural order" algorithm with ArrayObject::natcasesort.

Example


//example revised from php.net/*from  ww w .  java2s  .c om*/
<?php
$array = array('IMG0.png', 'img12.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png');

$arr1 = new ArrayObject($array);
$arr2 = clone $arr1;

$arr1->asort();
echo "Standard sorting\n";
print_r($arr1);

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

The code above generates the following result.





















Home »
  PHP Tutorial »
    Data Types »




Array
Array Associative
Array Util
ArrayObject
Data Types
Date
Date Format
DateTime
Number
String
String Escape
String Filter
String HTML
String Type
Timezone