15
10月
2015
$fruits =array(
0 => array( 'id' => 1, 'type' => '普通发票', 'weight' => 1, 'is_start' => 1),
1 => array( 'id' => 2, 'type' => '增值税专用发票', 'weight' => 2, 'is_start' => 1),
2 => array( 'id' => 3, 'type' => '收据', 'weight' => 0, 'is_start' => 1),
3 => array( 'id' => 4, 'type' => '测试', 'weight' => 4, 'is_start' => 0)
);
var_dump($fruits);
function compare($x,$y)
{
if($x['weight'] == $y['weight'])
return 0;
elseif($x['weight'] < $y['weight'])
return -1;
else
return 1;
}
usort($fruits,"compare");
echo $fruits[0]['id']."<br />";
echo $fruits[1]['id']."<br />";
echo $fruits[2]['id']."<br />";
echo $fruits[3]['id']."<br />";
0 => array( 'id' => 1, 'type' => '普通发票', 'weight' => 1, 'is_start' => 1),
1 => array( 'id' => 2, 'type' => '增值税专用发票', 'weight' => 2, 'is_start' => 1),
2 => array( 'id' => 3, 'type' => '收据', 'weight' => 0, 'is_start' => 1),
3 => array( 'id' => 4, 'type' => '测试', 'weight' => 4, 'is_start' => 0)
);
var_dump($fruits);
function compare($x,$y)
{
if($x['weight'] == $y['weight'])
return 0;
elseif($x['weight'] < $y['weight'])
return -1;
else
return 1;
}
usort($fruits,"compare");
echo $fruits[0]['id']."<br />";
echo $fruits[1]['id']."<br />";
echo $fruits[2]['id']."<br />";
echo $fruits[3]['id']."<br />";
特殊说明,本文版权归 ning个人博客 所有带原创标签请勿转载,转载请注明出处.
本文标题: php对二维数组排序