note 95258 deleted from function.array-multisort by cmb
| From: | cmb@php.net | Date: | Sun, 29 Dec 2019 10:26:01 +0000 |
| Subject: | note 95258 deleted from function.array-multisort by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213677@lists.php.net to get a copy of this message | ||
Note Submitter: d_inkubus at yahoo dot com
----
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