note 105797 added to function.asort
| From: | [DOT]) | Date: | Thu, 15 Sep 2011 22:32:58 +0000 |
| Subject: | note 105797 added to function.asort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-183203@lists.php.net to get a copy of this message | ||
This function can be used to sort multidimensional arrays with almost no work whatsoever by using
the individual values within the custom sort function.
This function passes the entire child element even if it is not a string. If it is an array, as
would be the case in multidimensional arrays, it will pass the whole child array as one parameter.
Therefore, do something elegant like this:
<?php
// Sort the multidimensional array
usort($results, "custom_sort");
// Define the custom sort function
function custom_sort($a,$b) {
return $a['some_sub_var']>$b['some_sub_var'];
}
?>
This does in 4 lines what other functions took 40 to 50 lines to do. This does not require you to
create temporary arrays or anything. This is, for me, a highly preferred solution.
Hope it helps!
----
Server IP: 69.147.83.197
Probable Submitter: 76.17.154.71
----
Manual Page -- http://www.php.net/manual/en/function.asort.php
Edit -- https://master.php.net/note/edit/105797
Del: integrated -- https://master.php.net/note/delete/105797/integrated
Del: useless -- https://master.php.net/note/delete/105797/useless
Del: bad code -- https://master.php.net/note/delete/105797/bad+code
Del: spam -- https://master.php.net/note/delete/105797/spam
Del: non-english -- https://master.php.net/note/delete/105797/non-english
Del: in docs -- https://master.php.net/note/delete/105797/in+docs
Del: other reasons-- https://master.php.net/note/delete/105797
Reject -- https://master.php.net/note/reject/105797
Search -- https://master.php.net/manage/user-notes.php