Pyh.conf’25: a new PHP conference for the Russian-speaking community

Voting

: seven plus two?
(Example: nine)

The Note You're Voting On

rojaro
21 years ago
Advanced sort array by second index function, which produces ascending (default) or descending output and uses optionally natural case insensitive sorting (which can be optionally case sensitive as well).
Only the first two arguments are required.

<?php

function sabsi ($array, $index, $order='asc', $natsort=FALSE, $case_sensitive=FALSE) {
if(
is_array($array) && count($array)>0) {
foreach(
array_keys($array) as $key) $temp[$key]=$array[$key][$index];
if(!
$natsort) ($order=='asc')? asort($temp) : arsort($temp);
else {
(
$case_sensitive)? natsort($temp) : natcasesort($temp);
if(
$order!='asc') $temp=array_reverse($temp,TRUE);
}
foreach(
array_keys($temp) as $key) (is_numeric($key))? $sorted[]=$array[$key] : $sorted[$key]=$array[$key];
return
$sorted;
}
return
$array;
}

?>

<< Back to user notes page

To Top