barkster Posted October 6, 2008 Share Posted October 6, 2008 I want to be able to sort an array by another array. I Found this website http://www.the-art-of-web.com/php/sortarray/ looks like what I want to do in section 6. Sorting based on a list of values but I have no idea what $b is and how to implement this? Any help would be appreciated. Thanks $data = array( array("name" => "Mary Johnson", "position" => "Secretary"), array("name" => "Amanda Miller", "position" => "Member"), array("name" => "James Brown", "position" => "Member"), array("name" => "Patricia Williams", "position" => "Member"), array("name" => "Michael Davis", "position" => "President"), array("name" => "Sarah Miller", "position" => "Vice-President"), array("name" => "Patrick Miller", "position" => "Member") ); $sortorder = array( 'President', 'Vice-President', 'Secretary', 'Member' ); function mySort($a, $b) { global $sortorder; if($a['position'] == $b['position']) { return 0; } $cmpa = array_search($a['position'], $sortorder); $cmpb = array_search($b['position'], $sortorder); return ($cmpa > $cmpb) ? 1 : -1; } Link to comment https://forums.phpfreaks.com/topic/127174-sort-array-by-array/ Share on other sites More sharing options...
genericnumber1 Posted October 6, 2008 Share Posted October 6, 2008 http://us3.php.net/usort That might be easier to understand. $a is one value, $b is the next value (in this example, you can name them whatever) you return 0 if the values are even, or -1 or 1 depending on whether the value $a is lower than $b or higher. Link to comment https://forums.phpfreaks.com/topic/127174-sort-array-by-array/#findComment-657918 Share on other sites More sharing options...
barkster Posted October 13, 2008 Author Share Posted October 13, 2008 Thanks, that's what I needed. I was missing the unsort command. Link to comment https://forums.phpfreaks.com/topic/127174-sort-array-by-array/#findComment-664091 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.