<?php
$arr = [
['class'=>'yiban','’sex'=>'男','name'=>'张三','score'=>90],
['class'=>'yiban','’sex'=>'男','name'=>'李四','score'=>80],
['class'=>'yiban','’sex'=>'男','name'=>'王五','score'=>98],
['class'=>'yiban','’sex'=>'男','name'=>'赵六','score'=>43],
['class'=>'yiban','’sex'=>'男','name'=>'钱八','score'=>68],
['class'=>'erban','’sex'=>'男','name'=>'德玛','score'=>76],
['class'=>'erban','’sex'=>'男','name'=>'张飞','score'=>98],
['class'=>'erban','’sex'=>'男','name'=>'关羽','score'=>83],
['class'=>'erban','’sex'=>'女','name'=>'貂蝉','score'=>72],
['class'=>'erban','’sex'=>'男','name'=>'吕布','score'=>49]
];
echo json_encode($arr,JSON_UNESCAPED_UNICODE);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<script>
var student_calss = [];//班级分组
$.post("class_api.php",function(data,status){
if(status == 'success'){
var jsonObj = JSON.parse(data) ;
for(var i = 0; i<jsonObj.length;i++){
if( !student_calss[jsonObj[i]['class']]) {
student_calss[jsonObj[i]['class'] ]=[];
}
// student_calss[jsonObj[i]['class']].push({'score':jsonObj[i]['score'] ,'name':jsonObj[i]['name'] });
student_calss[jsonObj[i]['class']].push([jsonObj[i]['score'],jsonObj[i]['name']]);
}
// console.log( student_calss );
for ( var x in student_calss) {
student_calss[x].sort(function(a,b){
return b[0]-a[0];
});
}
function print_array(arr){
for(var key in arr){
if(typeof(arr[key])=='array'||typeof(arr[key])=='object'){//递归调用
print_array(arr[key]);
}else{
document.write( arr[key] + '<br>');
}
}
}
console.log(print_array(student_calss));
}else{
alert('获取不到数据');
}
});
</script>
</body>
</html>