note 105115 modified in function.array-multisort by danbrown
| From: | danbrown@php.net | Date: | Thu, 28 Jul 2011 19:08:16 +0000 |
| Subject: | note 105115 modified in function.array-multisort by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-181971@lists.php.net to get a copy of this message | ||
if you want to sort an array by columns, this is the function to do it.
<?php
function array_csort() { //coded by Ichier2003
$args = func_get_args();
$marray = array_shift($args);
$msortline = 'return(array_multisort(';
foreach ($args as $arg) {
$i++;
if (is_string($arg)) {
foreach ($marray as $row) {
$sortarr[$i][] = $row[$arg];
}
} else {
$sortarr[$i] = $arg;
}
$msortline .= '$sortarr['.$i.'],';
}
$msortline .= '$marray));';
eval($msortline);
return $marray;
}
?>
--was--
since my old entry got deleted, because it was too long, and since i got so much feedback, i'll
post the array_csort again.
if you want to sort an array by columns, this is the function to do it.
explanation & example (was too long...) now here: http://www.ichier.de/coding/array_csort.html
<?php
function array_csort() { //coded by Ichier2003
$args = func_get_args();
$marray = array_shift($args);
$msortline = 'return(array_multisort(';
foreach ($args as $arg) {
$i++;
if (is_string($arg)) {
foreach ($marray as $row) {
$sortarr[$i][] = $row[$arg];
}
} else {
$sortarr[$i] = $arg;
}
$msortline .= '$sortarr['.$i.'],';
}
$msortline .= '$marray));';
eval($msortline);
return $marray;
}
?>
http://php.net/manual/en/function.array-multisort.php