关php二维数组中按某个元素的值排序 10

array([0]=>array(['id']=>1['type']=>'普通发票'['weight']=>1['is_start']=>1)[1]=>array(['i... 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)
)

我想重新排序数组,用weight的值来排序第一层
得到的结果的应该是如下这样:

array(
[0] => array(
['id'] => 3['type'] => '收据'['weight'] => 0['is_start'] => 1)
[1] => array(
['id'] => 1['type'] => '普通发票'['weight'] => 1['is_start'] => 1)
[2] => array(
['id'] => 2['type'] => '增值税专用发票'['weight'] => 2['is_start'] => 1)
[3] => array(
['id'] => 4['type'] => '测试'['weight'] => 4['is_start'] => 0)
)

假定这个数组的为$invoice,要如何去操作它?
PHP有很多数组函数可以利用,但是不知道哪个才是。
展开
 我来答
xing31120
2013-02-26 · TA获得超过577个赞
知道小有建树答主
回答量:260
采纳率:0%
帮助的人:237万
展开全部
$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 />";
caiaolin
2013-02-26 · TA获得超过664个赞
知道小有建树答主
回答量:621
采纳率:0%
帮助的人:458万
展开全部
参考手册的array_multisort()
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式