PHP根据json中的一个键值排序的
程序员文章站
2022-04-04 13:09:25
...
{
"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?
回复内容:
{
"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?
$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 $cache){
$cache = $arr['data'][$i]['level'];
$role = $arr['data'][$i]['role'];
}
}
最后读取$role就可以了
$t = json_decode('{
"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"}
]
}', true);
function levelSort($a, $b) {
if($a['level'] == $b['level']) return 0;
return ($a['level']
$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->data as $k => $v) {
$t[$v->level] = $v->role;//level做键,role做值
}
krsort($t);//按键逆序排序
$t = array_merge($t);//让键0打头
var_dump($t[0]);//第一个即最大的level的role
上一篇: jquery怎样隐藏某列元素
下一篇: jquery怎么移除td元素中的指定属性
推荐阅读
-
PHP中的一个很好用的文件上传类_PHP教程
-
PHP中设置一个严格30分钟过期Session面试题的4种答案_PHP
-
php数组中包含中文的排序方法_PHP
-
PHP中给AJAX传回的数据类型是JSON的一点困惑
-
php中的冒泡排序,取舍排序,插入排序
-
PHP,Mysql-根据一个给定经纬度的点,进行附近地点查询–合理利用算法,效率提高2125倍,mysql-2125倍_PHP教程
-
使用PHP导出Redis数据到另一个Redis中的代码_PHP
-
php 统计二维数组中某个相等值的总个数,并且组合成一个新的数组
-
php中的一些数组排序方法分享
-
问:一个天才的有关问题可以用php得到msn中好友的信息吗