note 60401 added to function.array-multisort
| From: | phpa-t-the-r-a-t-echir dot ag at osu1 dot php dot net | Date: | Thu, 05 Jan 2006 22:10:58 +0000 |
| Subject: | note 60401 added to function.array-multisort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-101274@lists.php.net to get a copy of this message | ||
Re: phu at kungphu, 19-Dec-2005 11:36
asort($test) will not let me specify which columns to sort ASC/DESC, NUMERIC/STRING etc.
I have data similar to what you specified. Now I want to sort $test by points DESC and name ASC.
Here's my function that does it, based on suggestions on this page. It uses array_multisort
(and hence acts just like it: preserving string-keys etc.)
<?php
function arrayColumnSort()
{
$n = func_num_args();
$ar = func_get_arg($n-1);
if(!is_array($ar))
return false;
for($i = 0; $i < $n-1; $i++)
$col[$i] = func_get_arg($i);
foreach($ar as $key => $val)
foreach($col as $kkey => $vval)
if(is_string($vval))
${"subar$kkey"}[$key] = $val[$vval];
$arv = array();
foreach($col as $key => $val)
$arv[] = (is_string($val) ? ${"subar$key"} : $val);
$arv[] = $ar;
call_user_func_array("array_multisort", $arv);
return $ar;
}
$test["pete"]['points']=1;
$test["pete"]['name']='Peter';
$test["mike"]['points']=5;
$test["mike"]['name']='Mike';
$test["zoo"]['points']=2;
$test["zoo"]['name']='John Zoo';
$test["ab"]['points']=2;
$test["ab"]['name']='John Ab';
$test1 = $test;
asort($test1);
$test2 = arrayColumnSort("points", SORT_DESC, SORT_NUMERIC, "name", SORT_ASC,
SORT_STRING, $test);
print_r($test1); // asort
print_r($test2); // arrayColumnSort
?>
Output from asort:
Array
(
[pete] => Array
(
[points] => 1
[name] => Peter
)
[ab] => Array
(
[points] => 2
[name] => John Ab
)
[zoo] => Array
(
[points] => 2
[name] => John Zoo
)
[mike] => Array
(
[points] => 5
[name] => Mike
)
)
Output from arrayColumnSort:
Array
(
[mike] => Array
(
[points] => 5
[name] => Mike
)
[ab] => Array
(
[points] => 2
[name] => John Ab
)
[zoo] => Array
(
[points] => 2
[name] => John Zoo
)
[pete] => Array
(
[points] => 1
[name] => Peter
)
)
----
Manual Page -- http://www.php.net/manual/en/function.array-multisort.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60401
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60401&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60401&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60401&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60401&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60401&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60401&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60401&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60401&report=yes
Search -- http://master.php.net/manage/user-notes.php