Jump to content

Sort Array by Array?


barkster

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.