我有一个多维数组,如下所示:
Array => (
[0] => Array(
[a] => abcd,
[b] => 22
),
[1] => Array(
[a] => defg,
[b] => 12
),
.....
)
我想通过内部数组中索引b
的值对此数组进行排序。如果我想按顺序对它进行排序,那么示例现在就可以了。但如果我想以升序排序,预期的输出将是:
Array => (
[0] => Array(
[a] => defg,
[b] => 12
),
[1] => Array(
[a] => abcd,
[b] => 22
),
.....
)
谢谢!
答案 0 :(得分:1)
您可以尝试此功能:
bool uasort ( array &$array , callback $cmp_function )
这是我的代码:
$arr = array(
0 => array('a' => 'abcd','b' => 22),
1 => array('a' => 'defg','b' => 12),
2 => array('a' => 'dfdf','b' => 32)
);
uasort($arr,'mul_sort');
function mul_sort($a,$b)
{
if($a['b'] > $b['b'])
return 1;//here,if you return -1,return 1 below,the result will be descending
if($a['b'] < $b['b'])
return -1;
if($a['b'] == $b['b'])
return 0;
}
print_r($arr);
output:
---------- PHP ----------
Array
(
[1] => Array
(
[a] => defg
[b] => 12
)
[0] => Array
(
[a] => abcd
[b] => 22
)
[2] => Array
(
[a] => dfdf
[b] => 32
)
)