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>";
}
?> 展开
<?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>";
}
?> 展开
展开全部
<?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
$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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
$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);
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);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
PHP有数组自定义排列函数,你可以自己去看文档。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<?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>";
}
?>
//这个比较简单点,保证没问题
$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>";
}
?>
//这个比较简单点,保证没问题
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询