note 101161 deleted from function.array-multisort by danbrown
| From: | danbrown@php.net | Date: | Tue, 30 Nov 2010 15:43:09 +0000 |
| Subject: | note 101161 deleted from function.array-multisort by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-174486@lists.php.net to get a copy of this message | ||
Note Submitter: mvadu
----
jimpoz's logic is plain and simple but does not work in php 5.3. New php expects each variable
to be a reference, and the code presented does not we get
Warning: Parameter 2 to array_multisort() expected to be a reference, value given
So here is the fixed version of his code:
<?php
function array_orderby()
{
$args = func_get_args();
$data = array_shift($args);
if (!is_array($data))
return false;
$multisortArgs=array();
foreach ($args as $index => $field)
{
if (is_string($field))
{
${"tmp_$index"} = array();
foreach ($data as $key => $row)
${"tmp_$index"}[$key] = $row[$field];
}
else
{
${"tmp_$index"} = $args[$index];
}
//add reference to arg array
$multisortArgs[$index] = &${"tmp_$index"};
}
//add reference to original data array to arg array
$multisortArgs[] = &$data;
//call array_multisort passing arg array
call_user_func_array('array_multisort', $multisortArgs);
return $data;
};
?>