PHP数组多个排序 - 由键值呢? [英] PHP array multiple sort - by value then by key?

查看:123
本文介绍了PHP数组多个排序 - 由键值呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用数字值串键中的标记与每个标签的出现的次数的列表的阵列,从而

I have an array of string keys with numeric values for use in a list of tags with the number of occurances of each tag, thus:

$arrTags['mango'] = 2; 
$arrTags['orange'] = 4; 
$arrTags['apple'] = 2; 
$arrTags['banana'] = 3;

这是这样我就可以这样降序次数顺序显示的标签列表:

this is so i can display the tag list in descending occurance order thus:

orange (4)  
banana (3) 
mango (2) 
apple (2)

我可以使用的 arsort 的由这是辉煌的,但我也希望有相同数值的标签,按照字母顺序排序,所以最终结果可能是值排序逆转:

i can use arsort to reverse sort by the value which is brilliant but i also want any tags that have the same numeric value to be sorted alphabetically, so the final result can be:

orange (4)  
banana (3) 
apple (2) 
mango (2)

有没有一种方法,我可以做到这一点?我猜的 usort 的可能是要走的路,但我看的例子在php.net和我茫然的眼神!非常感谢!

is there a way i can do this? i'm guessing that usort may be the way to go but i look at the examples on php.net and my eyes glaze over! many thanks!!!

推荐答案

看一看例子#3:
http://php.net/manual/en/function.array-multisort.php

您需要创建两个数组作为指标使用;一种制成了原始阵列的键和其他原始数组的值组成。

You'll need to create two arrays to use as indexes; one made up of the original array's keys and the other of the original array's values.

然后使用multisort由文本值(原始数组的键)进行排序,然后由数值(原始数组的值)。

Then use multisort to sort by text values (keys of the original array) and then by the numeric values (values of the original array).

这篇关于PHP数组多个排序 - 由键值呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆