为了账号安全,请及时绑定邮箱和手机立即绑定

PHP根据json中的一个键值排序的

PHP根据json中的一个键值排序的

慕姐8265434 2019-04-13 08:45:56
{"data":[{"roleId":"qai41","role":"qai43","level":"45","sex":"0"},{"roleId":"qai41","role":"qai41","level":"78","sex":"0"},{"roleId":"qai41","role":"qai42","level":"44","sex":"0"}]}怎么取出level最大的role?
查看完整描述

2 回答

?
互换的青春

TA贡献1797条经验 获得超6个赞

$str='{
"data":[
{"roleId":"qai41","role":"qai43","level":"45","sex":"0"},
{"roleId":"qai41","role":"qai41","level":"78","sex":"0"},
{"roleId":"qai41","role":"qai42","level":"44","sex":"0"}
]
}';
$arr=json_decode($str);
foreach($arr->dataas$k=>$v){
$t[$v->level]=$v->role;//level做键,role做值
}
krsort($t);//按键逆序排序
$t=array_merge($t);//让键0打头
var_dump($t[0]);//第一个即最大的level的role
                            
查看完整回答
反对 回复 2019-04-13
?
繁花不似锦

TA贡献1851条经验 获得超4个赞

$arr={
"data":[
{"roleId":"qai41","role":"qai43","level":"45","sex":"0"},
{"roleId":"qai41","role":"qai41","level":"78","sex":"0"},
{"roleId":"qai41","role":"qai42","level":"44","sex":"0"}
]
};
$cache=0;
$role='';
for($i=0;$i<3;$i++){
if($arr['data'][$i]['level']>$cache){
$cache=$arr['data'][$i]['level'];
$role=$arr['data'][$i]['role'];
}
}
最后读取$role就可以了
                            
查看完整回答
反对 回复 2019-04-13
  • 2 回答
  • 0 关注
  • 1516 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信