Voting

: min(one, six)?
(Example: nine)

The Note You're Voting On

@gmail bereikme
18 years ago
Here's a handy function to sort an array on 1 or more columns using natural sort:
<?php
// Example: $records = columnSort($records, array('name', 'asc', 'addres', 'desc', 'city', 'asc'));

$globalMultisortVar = array();
function
columnSort($recs, $cols) {
global
$globalMultisortVar;
$globalMultisortVar = $cols;
usort($recs, 'multiStrnatcmp');
return(
$recs);
}

function
multiStrnatcmp($a, $b) {
global
$globalMultisortVar;
$cols = $globalMultisortVar;
$i = 0;
$result = 0;
while (
$result == 0 && $i < count($cols)) {
$result = ($cols[$i + 1] == 'desc' ? strnatcmp($b[$cols[$i]], $a[$cols[$i]]) : $result = strnatcmp($a[$cols[$i]], $b[$cols[$i]]));
$i+=2;
}
return
$result;
}

?>

Greetings,

- John

<< Back to user notes page

To Top