如何对数组内的数组进行排序? [英] How to sort arrays inside an array?

查看:103
本文介绍了如何对数组内的数组进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据数组的数组值之一对数组内的数组进行排序.

I need to sort arrays inside an array of array based on one of the array's array value.

例如:

$data = array( array( 1, "Article One", 132, 12402773, 3 ),
               array( 2, "Article Two", 251, 12519283, 5 ),
               array( 3, "Article Three", 107, 12411321, 3 ),
               array( 4, "Article Four", 501, 12228135, 4 ) );

默认情况下,如果我打印每个数组的第二个元素:

By default, if I print the 2nd element of each array:

  • 第一条
  • 第二条
  • 第三条
  • 第四条

我需要按子数组的第3个元素按降序对其进行排序.

I need to sort it in a descending order by the 3rd element of the child-array.

所以会是这样:

  • 第四条
  • 第二条
  • 第一条
  • 第三条

因为501> 251> 132> 107.

Because 501 > 251 > 132 > 107.

有什么建议吗?

推荐答案

我通常为此使用usort():

function compare($a, $b) {
    return ($a[2] > $b[2]);
}

usort($data, 'compare');

这篇关于如何对数组内的数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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