note 95258 added to function.array-multisort
| From: | d_inkubus at yahoo dot com | Date: | Mon, 21 Dec 2009 23:33:26 +0000 |
| Subject: | note 95258 added to function.array-multisort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-164259@lists.php.net to get a copy of this message | ||
Update to array_msort()--in PHP 5.3 it won't work.
The solution is to replace the array_merge($params, $order) with the following code snippet:
<?php
$order=(array)$order;
foreach($order as $order_element)
{
$params[]=&$order_element;
}
?>
Full code to array_msort():
<?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];
$order=(array)$order;
foreach($order as $order_element)
{
//pass by reference, as required by php 5.3
$params[]=&$order_element;
}
}
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;
}
?>
For further reference, see these bug reports:
http://bugs.php.net/bug.php?id=49069
http://bugs.php.net/bug.php?id=49241
----
Server IP: 69.147.83.197
Probable Submitter: 128.223.201.168
----
Manual Page -- http://www.php.net/manual/en/function.array-multisort.php
Edit -- https://master.php.net/note/edit/95258
Del: integrated -- https://master.php.net/note/delete/95258/integrated
Del: useless -- https://master.php.net/note/delete/95258/useless
Del: bad code -- https://master.php.net/note/delete/95258/bad+code
Del: spam -- https://master.php.net/note/delete/95258/spam
Del: non-english -- https://master.php.net/note/delete/95258/non-english
Del: in docs -- https://master.php.net/note/delete/95258/in+docs
Del: other reasons-- https://master.php.net/note/delete/95258
Reject -- https://master.php.net/note/reject/95258
Search -- https://master.php.net/manage/user-notes.php