使用子键值对PHP数组排序 [英] Sorting PHP array using subkey-values
问题描述
我有一个看起来像这样的数组:
I have an array that looks something like this:
Array
(
[Erik] => Array
(
[count] => 10
[changes] => 1
)
[Morten] => Array
(
[count] => 8
[changes] => 1
)
)
现在,数组中的键是我们帮助台系统中技术人员的姓名.我试图根据[count]
加[changes]
的数量对此进行排序,然后显示它们.我尝试使用 usort ,但随后将数组键替换为索引号.如何排序并保留数组键?
Now, the keys in the array are names of technicians in our Helpdesk-system. I'm trying to sort this based on number of [count]
plus [changes]
and then show them. I've tried to use usort, but then the array keys are replaced by index numbers. How can I sort this and keep the array keys?
推荐答案
您应该为此使用uasort
.
bool uasort(array& $ array,回调 $ cmp_function)
bool uasort ( array &$array , callback $cmp_function )
此函数对数组进行排序,使得 数组索引保持其 与数组元素的相关性 它们与使用 用户定义的比较功能. 主要在排序时使用 实际关联数组 元素顺序很重要.
This function sorts an array such that array indices maintain their correlation with the array elements they are associated with, using a user-defined comparison function. This is used mainly when sorting associative arrays where the actual element order is significant.
这篇关于使用子键值对PHP数组排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!