1.在前端页面用js处理
转载https://www.cnblogs.com/rysinal/p/5834446.html
为了第二种方法对比,特意把id 转化为pid
var arr = [ {"pid":"1001","name":"值1","value":"111"}, {"pid":"1001","name":"值1","value":"11111"}, {"pid":"1002","name":"值2","value":"25462"}, {"pid":"1002","name":"值2","value":"23131"}, {"pid":"1002","name":"值2","value":"2315432"}, {"pid":"1003","name":"值3","value":"333333"} ]; var map = {}, dest = []; for(var i = 0; i < arr.length; i++){ var ai = arr[i]; if(!map[ai.pid]){ dest.push({ pid: ai.pid, name: ai.name, data: [ai] }); map[ai.pid] = ai; }else{ for(var j = 0; j < dest.length; j++){ var dj = dest[j]; if(dj.pid == ai.pid){ dj.data.push(ai); break; } } } } console.log(dest);
第二种,在后台处理(框架tp5)
foreach($res as $key=>$v) { $list[$v['pid']][] = $v; }