note 89918 added to function.array-multisort
| From: | mech dot cx at osu1 dot php dot net | Date: | Sat, 28 Mar 2009 20:04:46 +0000 |
| Subject: | note 89918 added to function.array-multisort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-152560@lists.php.net to get a copy of this message | ||
I was (as near everyone here :-) looking to sort 2-dimensional arrays by certain fields in the
associative sub-arrays.
What I didn't like about the documentation examples is that you need to loop through the input
array to create sub arrays first, then use those in the function call.
"php a-t-the-r-a-t-e chir.ag"
(http://www.php.net/manual/en/function.array-multisort.php#60401) wrote a quite cunning wrapper
function, I rewrote it slightly, changing variable names and adding comments (for my sanity :-)
mostly.
One snag I found: the input array is passed to array_multisort as last argument, but the changed
array is not the one that is returned. Passing it by reference fixed that. This seems to be caused
by the whole thing sitting inside the call_user_func_array, as shown below.
<?php
$points = array(1, 5, 2, 2);
$names = array('peter', 'mike', 'john Zoo', 'john Ab');
$source = array (
array ( 'points' => 1, 'name' => 'Peter'),
array ( 'points' => 5, 'name' => 'Mike'),
array ( 'points' => 2, 'name' => 'John Zoo'),
array ( 'points' => 2, 'name' => 'John Ab')
);
call_user_func_array('array_multisort', array($points, SORT_DESC, SORT_NUMERIC, $names,
SORT_ASC, SORT_STRING, $source)); // doesn't work
print_r($source);
call_user_func_array('array_multisort', array($points, SORT_DESC, SORT_NUMERIC, $names,
SORT_ASC, SORT_STRING, &$source)); // works!
print_r($source);
// Call like arrayColumnSort('points', SORT_DESC, SORT_NUMERIC, 'name',
SORT_ASC, SORT_STRING, $source);
// Slightly adapted from http://www.php.net/manual/en/function.array-multisort.php#60401
// arrayColumnSort(string $field, [options, ], string $field2, [options, ], .... , $array) /
//____________________
// arrayColumnSort() /
function arrayColumnSort() {
$args = func_get_args();
$array = array_pop($args);
if (! is_array($array)) return false;
// Here we'll sift out the values from the columns we want to sort on, and put them in
numbered 'subar' ("sub-array") arrays.
// (So when sorting by two fields with two modifiers (sort options) each, this will create
$subar0 and $subar3)
foreach($array as $key => $row) // loop through source array
foreach($args as $akey => $val) // loop through args (fields and modifiers)
if(is_string($val)) // if the arg's a field, add its value from the source
array to a sub-array
${"subar$akey"}[$key] = $row[$val];
// $multisort_args contains the arguments that would (/will) go into array_multisort():
sub-arrays, modifiers and the source array
$multisort_args = array();
foreach($args as $key => $val)
$multisort_args[] = (is_string($val) ? ${"subar$key"} : $val);
$multisort_args[] = &$array; // finally add the source array, by reference
call_user_func_array("array_multisort", $multisort_args);
return $array;
}
?>
----
Server IP: 83.137.20.135
Probable Submitter: 94.212.43.63
----
Manual Page -- http://www.php.net/manual/en/function.array-multisort.php
Edit -- https://master.php.net/note/edit/89918
Del: integrated -- https://master.php.net/note/delete/89918/integrated
Del: useless -- https://master.php.net/note/delete/89918/useless
Del: bad code -- https://master.php.net/note/delete/89918/bad+code
Del: spam -- https://master.php.net/note/delete/89918/spam
Del: non-english -- https://master.php.net/note/delete/89918/non-english
Del: in docs -- https://master.php.net/note/delete/89918/in+docs
Del: other reasons-- https://master.php.net/note/delete/89918
Reject -- https://master.php.net/note/reject/89918
Search -- https://master.php.net/manage/user-notes.php