我有一个数组,格式如下:
$list = [ ["fdsa","1","fdsa"],["sadf","0","asdf"],["frfrf","0","sadfdsf"] ]
如何$list
根据每个内部数组的第一个值按字母顺序排列?
谢谢!
我有一个数组,格式如下:
$list = [ ["fdsa","1","fdsa"],["sadf","0","asdf"],["frfrf","0","sadfdsf"] ]
如何$list
根据每个内部数组的第一个值按字母顺序排列?
谢谢!
我有另一个答案的这个功能,但可以修改它来做同样的事情:
// This sorts simply by alphabetic order
function reindex( $a, $b )
{
// Here we grab the values of the 'code' keys from within the array.
$val1 = $a[0];
$val2 = $b[0];
// Compare string alphabetically
if( $val1 > $val2 ) {
return 1;
} elseif( $val1 < $val2 ) {
return -1;
} else {
return 0;
}
}
// Call it like this:
usort( $array, 'reindex' );
print_r( $array );
<?php
asort($list);
// OR
array_multisort($list);
?>
PHP手册:asort()
和array_multisort()
function order($a, $b){
if ($a[0] == $b[0]) {
return 0;
}
return ($a[0] < $b[0]) ? -1 : 1;
}
$list = [ ["fdsa","1","fdsa"],["sadf","0","asdf"],["frfrf","0","sadfdsf"] ];
usort($list, "order");
var_dump($list); die;
您需要使用自己的比较函数进行排序,并将其与usort()
.