Sort the entries by keys using a user-defined comparison function with ArrayObject::uksort in PHP
Description
The following code shows how to sort the entries by keys using a user-defined comparison function with ArrayObject::uksort.
Example
//example revised from php.net// w ww. j ava 2 s. c om
<?php
function cmp($a, $b) {
$a = preg_replace('@^(a|an|the) @', '', $a);
$b = preg_replace('@^(a|an|the) @', '', $b);
return strcasecmp($a, $b);
}
$array = array("John" => 1, "the Earth" => 2, "an apple" => 3, "a banana" => 4);
$arrayObject = new ArrayObject($array);
$arrayObject->uksort('cmp');
foreach ($arrayObject as $key => $value) {
echo "$key: $value\n";
}
?>
The code above generates the following result.