Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

multidimensional array - php - usort or array_multisort?

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 
        )
)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since this is the most prominent Google result on array_multisort vs usort, I'm going to reply even though it's 4 years old.

usort() is more concise and doesn't require extracting a column array to feed to array_multisort(). (It also does less than array_multisort.)

However, when I repeatedly tested it today on arrays of 20,000 and 10,000 representative data rows, usort() was 7-15x slower than array_multisort() when the column was random values of type int and the column was pre-extracted. That is as one might expect, since for every comparison you're comparing an entire php function call to optimized intrinsic code.

Using an anonymous function as in the previous reply gave a 30-35% improvement over passing usort() the name of a defined function. It was never better than 8x slower and usually worse than 10x slower. Often this won't matter, but when you start getting up into tenths of a second of CPU time just for sorting one array, it might. Extracting the column without array_column() on a pre-5.5 server never quite halved the differential at best.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...