PHP 多维数组排序
我有以下数组,它当前是按实体计数排序的(由在 cakephp 中完成的查询输出 - 我只想要前几个实体),我现在想对 Entity->title 的数组进行排序。
我尝试使用array_multisort
执行此操作,但失败了。这可能吗?
Array
(
[0] => Array
(
[Entity] => Array
(
[title] => Orange
)
[0] => Array
(
[entitycount] => 76
)
)
[1] => Array
(
[Entity] => Array
(
[title] => Apple
)
[0] => Array
(
[entitycount] => 78
)
)
[2] => Array
(
[Entity] => Array
(
[title] => Lemon
)
[0] => Array
(
[entitycount] => 85
)
)
)
I have the following array, it is currently created sorted by entity_count (outputted by a query done in cakephp - I only wanted the top few entities), I want to now sort the array for the Entity->title.
I tried doing this with array_multisort
but failed. Is this possible?
Array
(
[0] => Array
(
[Entity] => Array
(
[title] => Orange
)
[0] => Array
(
[entitycount] => 76
)
)
[1] => Array
(
[Entity] => Array
(
[title] => Apple
)
[0] => Array
(
[entitycount] => 78
)
)
[2] => Array
(
[Entity] => Array
(
[title] => Lemon
)
[0] => Array
(
[entitycount] => 85
)
)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样创建一个回调函数:
然后运行 array_map 和多重排序
Create a callback function like so:
Then run it thew an array_map and multi sort
试试这个:
这里
array_map
和array_map
net/functions.anonymous" rel="nofollow">匿名函数(自 PHP 5.3 起可用,您可以使用create_function
(在之前的版本中)用于获取标题数组,然后用于根据标题对数组进行排序。Try this:
Here
array_map
and an anonymous function (available since PHP 5.3, you can usecreate_function
in prior versions) is used to get an array of the titles that is then used to sort the array according to their titles.您需要编写一个自定义比较函数,然后使用 usort 。
使用以下方式调用它:
You need to write a custom compare function and then use usort .
Calll it using: