php中的 foreach 对数组重新排序 很简单

我想根据"rating"的数值重新按照大小排列,具体怎么操作啊,方法尽量简单哦。。<?php$data=array(array("id"=>1,"name"=>"Bone... 我想根据"rating" 的数值重新按照大小排列,具体怎么操作啊,方法尽量简单哦。。

<?php
$data = array(
array("id" => 1, "name" => "Boney M", "rating" => 3),
array("id" => 2, "name" => "Take That", "rating" => 1),
array("id" => 3, "name" => "The Killers", "rating" => 4),
array("id" => 4, "name" => "Lusain", "rating" => 3),
);
foreach ($data as $key => $value) {
echo $value['id'];
echo $value['name'];
echo $value['rating'];
echo "<br>";
}

?>
展开
 我来答
百度网友9308069
2012-01-19 · TA获得超过1万个赞
知道大有可为答主
回答量:3947
采纳率:89%
帮助的人:1934万
展开全部
<?php
$data = array(
array("id" => 1, "name" => "Boney M", "rating" => 3),
array("id" => 2, "name" => "Take That", "rating" => 1),
array("id" => 3, "name" => "The Killers", "rating" => 4),
array("id" => 4, "name" => "Lusain", "rating" => 3),
);
//排序
function cmp(&$a,&$b){ return $a['rating']-$b['rating']; }
usort($data,'cmp');
var_dump($data);
=======
array
0 =>
array
'id' => int 2
'name' => string 'Take That' (length=9)
'rating' => int 1
1 =>
array
'id' => int 1
'name' => string 'Boney M' (length=7)
'rating' => int 3
2 =>
array
'id' => int 4
'name' => string 'Lusain' (length=6)
'rating' => int 3
3 =>
array
'id' => int 3
'name' => string 'The Killers' (length=11)
'rating' => int 4
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友5cc3cfb
2018-03-23
知道答主
回答量:22
采纳率:0%
帮助的人:3.5万
展开全部
$array=array(12,3,23,67,45,8);
foreach($array as $v2=>$o2){
foreach($array as $v=>$o){
if($o>$zhi){
$zhi=$o;
}
}
unset($array[array_search($zhi,$array)]);
$iff[]=$zhi;
$zhi='';
}
dump($iff);
//从小到大
$zhi='9999';
$array=array(12,3,23,67,45,8);
foreach($array as $v2=>$o2){
foreach($array as $v=>$o){
if($o<$zhi){
$zhi=$o;
}
}
unset($array[array_search($zhi,$array)]);
$iff[]=$zhi;
$zhi='9999';
}
第二种
foreach($node as $k=>$v){
$dos[$k]=$v['sort'];
}
array_multisort($dos,SORT_ASC,$node);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
隋龙飞
2012-01-20 · TA获得超过511个赞
知道小有建树答主
回答量:380
采纳率:0%
帮助的人:250万
展开全部
PHP有数组自定义排列函数,你可以自己去看文档。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
卓少学长
推荐于2016-11-14 · TA获得超过113个赞
知道小有建树答主
回答量:93
采纳率:0%
帮助的人:99.2万
展开全部
<?php
$data = array(
array("id" => 1, "name" => "Boney M", "rating" => 3),
array("id" => 2, "name" => "Take That", "rating" => 1),
array("id" => 3, "name" => "The Killers", "rating" => 4),
array("id" => 4, "name" => "Lusain", "rating" => 3),
);

foreach ($data as $key => $value) {
$rating[$key] = $value['rating'];
}

array_multisort($rating, SORT_ASC, $data);

foreach ($data as $key => $value) {
echo $value['id'];
echo $value['name'];
echo $value['rating'];
echo "<br>";
}
?>

//这个比较简单点,保证没问题
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 3条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式