note 91645 added to function.array-multisort
| From: | cagret at gmail dot com | Date: | Sun, 21 Jun 2009 10:38:19 +0000 |
| Subject: | note 91645 added to function.array-multisort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-157192@lists.php.net to get a copy of this message | ||
Update to array_msort(): instead of eval() using call_user_func_array(). If we get column sorting
via $_GET it would be a serious security hole.
Now it also accepts multiple options for each column:
<?php
$arr2 = array_msort($arr1, array('name'=>array(SORT_DESC,SORT_REGULAR),
'cat'=>SORT_ASC));
?>
Btw. this function is automatically case-insensitive, as you can see use of strtolower().
<?php
function array_msort($array, $cols)
{
$colarr = array();
foreach ($cols as $col => $order) {
$colarr[$col] = array();
foreach ($array as $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); }
}
$params = array();
foreach ($cols as $col => $order) {
$params[] =& $colarr[$col];
$params = array_merge($params, (array)$order);
}
call_user_func_array('array_multisort', $params);
$ret = array();
$keys = array();
$first = true;
foreach ($colarr as $col => $arr) {
foreach ($arr as $k => $v) {
if ($first) { $keys[$k] = substr($k,1); }
$k = $keys[$k];
if (!isset($ret[$k])) $ret[$k] = $array[$k];
$ret[$k][$col] = $array[$k][$col];
}
$first = false;
}
return $ret;
}
?>
----
Server IP: 92.48.74.199
Probable Submitter: 83.31.234.112
----
Manual Page -- http://www.php.net/manual/en/function.array-multisort.php
Edit -- https://master.php.net/note/edit/91645
Del: integrated -- https://master.php.net/note/delete/91645/integrated
Del: useless -- https://master.php.net/note/delete/91645/useless
Del: bad code -- https://master.php.net/note/delete/91645/bad+code
Del: spam -- https://master.php.net/note/delete/91645/spam
Del: non-english -- https://master.php.net/note/delete/91645/non-english
Del: in docs -- https://master.php.net/note/delete/91645/in+docs
Del: other reasons-- https://master.php.net/note/delete/91645
Reject -- https://master.php.net/note/reject/91645
Search -- https://master.php.net/manage/user-notes.php