note 101161 added to function.array-multisort
| From: | mvadu at osu1 dot php dot net | Date: | Tue, 30 Nov 2010 15:18:54 +0000 |
| Subject: | note 101161 added to function.array-multisort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-174481@lists.php.net to get a copy of this message | ||
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;
};
?>
----
Server IP: 69.147.83.197
Probable Submitter: 192.223.243.6
----
Manual Page -- http://www.php.net/manual/en/function.array-multisort.php
Edit -- https://master.php.net/note/edit/101161
Del: integrated -- https://master.php.net/note/delete/101161/integrated
Del: useless -- https://master.php.net/note/delete/101161/useless
Del: bad code -- https://master.php.net/note/delete/101161/bad+code
Del: spam -- https://master.php.net/note/delete/101161/spam
Del: non-english -- https://master.php.net/note/delete/101161/non-english
Del: in docs -- https://master.php.net/note/delete/101161/in+docs
Del: other reasons-- https://master.php.net/note/delete/101161
Reject -- https://master.php.net/note/reject/101161
Search -- https://master.php.net/manage/user-notes.php