- var_dump: documentation ( source)
- krsort: documentation ( source)
- strtolower: documentation ( source)
- ksort: documentation ( source)
<?php
$array = Array
(
'3' => 10,
'2' => 2,
'5' => 'Nakiya',
);
/**
* Sort an array by key.
*/
function sortbyKeys($array, $direction = 'ASC')
{
$direction = (strtolower($direction) === 'desc') ? SORT_DESC : SORT_ASC;
if ($direction === SORT_ASC) {
ksort($array);
} else {
krsort($array);
}
return $array;
}
var_dump(sortbyKeys($array));