note 105798 added to function.array-multisort

From: Date: Thu, 15 Sep 2011 22:36:20 +0000
Subject: note 105798 added to function.array-multisort
Groups: php.notes 
Request: Send a blank email to php-notes+get-183204@lists.php.net to get a copy of this message
USort 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 over this function. Hope it helps! ---- Server IP: 64.71.164.2 Probable Submitter: 76.17.154.71 ---- Manual Page -- http://www.php.net/manual/en/function.array-multisort.php Edit -- https://master.php.net/note/edit/105798 Del: integrated -- https://master.php.net/note/delete/105798/integrated Del: useless -- https://master.php.net/note/delete/105798/useless Del: bad code -- https://master.php.net/note/delete/105798/bad+code Del: spam -- https://master.php.net/note/delete/105798/spam Del: non-english -- https://master.php.net/note/delete/105798/non-english Del: in docs -- https://master.php.net/note/delete/105798/in+docs Del: other reasons-- https://master.php.net/note/delete/105798 Reject -- https://master.php.net/note/reject/105798 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#183204) next »