views:

95

answers:

1

Trying to sort the array below by memnum in ascending order, and I'm a bit confused which is better to use... usort or array_multisort? I was thinking usort because it's multidimensional? Does anyone have an example of this?

Array
(
    [0] => Array
        (
            [memnum] => 3236467423
            [mid] => 1104881300  
            [fname] => JOHN        
            [lname] => DOE                 
            [add1] =>  OMITTED
            [add2] =>             
            [city] => CHESTERFIELD      
            [state] => MI
            [zip] => 48051
            [age] => 50 
        )
    [1] => Array
        (
            [memnum] => 3258467922
            [mid] => 1105121457  
            [fname] => JANE        
            [lname] => DOE                 
            [add1] =>  OMITTED
            [add2] =>             
            [city] => CHESTERFIELD      
            [state] => MI
            [zip] => 48051
            [age] => 50 
        )
    [2] => Array
        (
            [memnum] => 3237769108
            [mid] => 1104489312  
            [fname] => BOB        
            [lname] => DOE                 
            [add1] =>  OMITTED
            [add2] =>             
            [city] => CHESTERFIELD      
            [state] => MI
            [zip] => 48051
            [age] => 50 
        )
)
A: 

Just usort:

usort($arr, function (array $a, array $b) { return $a["memnum"] - $b["memnum"]; });

array_multisort is used to compare elements from different arrays (or sub-arrays) at the same time. You want to compare elements of only one array, so you use usort. The fact that those elements are themselves arrays is irrelevant.

Artefacto