3 回答
TA贡献1803条经验 获得超6个赞
试试这个usort函数:
function cmp($a, $b){
if ($a == $b)
return 0;
return ($a['name'] < $b['name']) ? -1 : 1;
}
$my_array = array(
0 => array
(
'type' => 'suite'
,'name' => 'A-Name'
)
,1 => array
(
'type' => 'suite'
,'name' => 'C-Name'
)
,2 => array
(
'type' => 'suite'
,'name' => 'B-Name'
)
);
usort($my_array, "cmp");
如果在类中使用它,则第二个参数将更改为如下数组:
usort($my_array, array($this,'cmp'));
TA贡献1946条经验 获得超3个赞
<?php
$a=array(
array('a'=>5,'b'=>7),array('c'=>4,'d'=>2),array('e'=>0,'f'=>12)
);
function cmp_sort($x,$y){ //your function to compare two keys
if($x===$y)
return 0;
else
return ($x<$y?1:-1);
}
uasort($a,'cmp_sort'); //call user-defined compare function
print_r($a); //printing the sorted array
?>
输出=>
数组([2] =>数组([e] => 0 [f] => 12)[1] =>数组([c] => 4 [d] => 2)[0] =>数组([ a] => 5 [b] => 7))
- 3 回答
- 0 关注
- 500 浏览
添加回答
举报