我有一个看起来像这样的数组:
<前>大批([埃里克] => 数组([计数] => 10[变化] => 1)[Morten] => 数组([计数] => 8[变化] => 1))现在,数组中的键是我们帮助台系统中技术人员的姓名.我正在尝试根据 [count] 加上 [changes] 的数量对其进行排序,然后显示它们.我尝试使用 usort,但随后数组键被索引号替换.我该如何排序并保留数组键?
你应该使用 uasort 来解决这个问题.
bool uasort ( array &$array ,回调$cmp_function )
这个函数对一个数组进行排序,使得数组索引保持它们的与数组元素的相关性它们与,使用用户定义的比较函数.这主要用于排序时关联数组,其中实际元素顺序很重要.
I have an array that looks something like this:
Array
(
[Erik] => Array
(
[count] => 10
[changes] => 1
)
[Morten] => Array
(
[count] => 8
[changes] => 1
)
)
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?
You should use uasort for this.
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 数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
Zend 中的动作视图助手 - 解决方法?Action View Helper in Zend - Work around?(Zend 中的动作视图助手 - 解决方法?)
这是将 URI 与 PHP 中用于 MVC 的类/方法匹配的好方Is this a good way to match URI to class/method in PHP for MVC(这是将 URI 与 PHP 中用于 MVC 的类/方法匹配的好方法吗)
我在哪里保存 Zend Framework 中的部分(视图),以便Where do I save partial (views) in Zend Framework, to be accessible for all Views in my App?(我在哪里保存 Zend Framework 中的部分(视图),以
有一个网站的单一入口点.坏的?好的?没问题?Having a single entry point to a website. Bad? Good? Non-issue?(有一个网站的单一入口点.坏的?好的?没问题?)
MVC + 服务层在 Zend 或 PHP 中常见吗?Is MVC + Service Layer common in zend or PHP?(MVC + 服务层在 Zend 或 PHP 中常见吗?)
PHP MVC 方法中的 Hello World 示例Hello World example in MVC approach to PHP(PHP MVC 方法中的 Hello World 示例)