如何根据数组中的特定字段对数组进行排序? [英] How to sort an array based on a specific field in the array?
本文介绍了如何根据数组中的特定字段对数组进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何根据数组中的两个特定值对数组进行排序?例如:
How can I sort an array based on two specific values within the array? For instance:
$arr = array(
array('a' => array('field1' => 'Abc', 'field2' => 'Def'), 'b' => 0)
array('a' => array('field1' => 'Ghi', 'field2' => 'Jkl'), 'b' => 0)
);
我想根据 $arr[$i]['a']['field1']
变量对这个数组进行排序.我该怎么做?
I want to sort this array based on the $arr[$i]['a']['field1']
variable. How can I do that?
推荐答案
试试这个:
function cmp($a, $b) {
if ($a['a']['field1'] == $b['a']['field1'] )
return 0;
return ( $a['a']['field1'] < $b['a']['field1'] ) ? -1 : 1;
}
uasort($arr, 'cmp');
这只是对 PHP 文档页面上提供的示例稍作改动:http://www.php.net/manual/en/function.uasort.php
This is just a slight alteration of the example provided on the PHP documentation page: http://www.php.net/manual/en/function.uasort.php
这篇关于如何根据数组中的特定字段对数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文