简体   繁体   English

php - usort 或 array_multisort?

[英]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?试图按 memnum 按升序对下面的数组进行排序,我有点困惑,哪个更好用... usort 或 array_multisort? I was thinking usort because it's multidimensional?我在想 usort 因为它是多维的? 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 
        )
)

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.由于这是 array_multisort 与 usort 上最突出的谷歌结果,即使它已有 4 年历史,我也会回复。

usort () is more concise and doesn't require extracting a column array to feed to array_multisort (). usort () 更简洁,不需要提取列数组来提供给array_multisort ()。 (It also does less than array_multisort.) (它也比 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.但是,当我今天在包含 20,000 和 10,000 个代表性数据行的数组上反复测试它时,当列是 int 类型的随机值并且该列被预先提取时, usort () 比array_multisort () 慢 7-15 倍。 That is as one might expect, since for every comparison you're comparing an entire php function call to optimized intrinsic code.正如人们所期望的那样,因为对于每次比较,您都是在将整个 php 函数调用与优化的内部代码进行比较。

Using an anonymous function as in the previous reply gave a 30-35% improvement over passing usort () the name of a defined function.使用前面回复中的匿名函数比传递usort () 已定义函数的名称提高了 30-35%。 It was never better than 8x slower and usually worse than 10x slower.它永远不会比慢 8 倍更好,通常比慢 10 倍更糟糕。 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.通常这无关紧要,但是当您开始占用十分之一秒的 CPU 时间仅用于对一个数组进行排序时,它可能会发生。 Extracting the column without array_column () on a pre-5.5 server never quite halved the differential at best.在 5.5 之前的服务器上提取没有array_column () 的列充其量也不会将差异减半。

Just usort :只是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. array_multisort用于同时比较来自不同数组(或子数组)的元素。 You want to compare elements of only one array, so you use usort .您只想比较一个数组的元素,因此您使用usort The fact that those elements are themselves arrays is irrelevant.这些元素本身就是数组这一事实是无关紧要的。

为了记录(并与usort解决方案进行比较),这里是如何使用array_multisort

array_multisort(array_column($arr, 'memnum'), $arr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM