Trying to sort by this multidimensional array by screen name:
//Looping through this for each member of a group
unset($member_info);
$member_info->id = $member->id;
$member_info->screen_name = $member->screen_name;
$member_info->first_name = $member->first_name;
$member_info->last_name = $member->last_name;
$member_info->email = $member->email;
//Sort member_info by screen name
$member_array = sort_by_array_key($member_info,$member_info->screen_name,'ASC');
$member_list[] = $member_array;
Sort function:
function sort_by_array_key($array,$sort_key,$dir = 'ASC') {
usort($array, function($a,$b) use ($sort_key){
return strnatcasecmp($a["$sort_key"], $b["$sort_key"]);
});
if ( $dir == 'DESC' ) { return array_reverse($array); }
else { return $array; }
}
As an example, screen names are currently in this order even after I run the sort function:
newUser
Chris
Carlos
Jason
I am probably applying the sort at the wrong time but after looking at other similar examples here I was having trouble with this, any help is appreciated, thanks!
First of all, you’re sorting an individual item there, where you probably want to sort the actual list, $member_list. Also, you’re trying to sort an array where you initialize an object. Either initialize an array or sort an object.
If I assume you really want to play with arrays, like you imply on the subject, this might be something you’re trying to do: