php数据优化
Posted 我很迷茫
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php数据优化相关的知识,希望对你有一定的参考价值。
Array ( [0] => Array ( [id] => 19 [receive_id] => 41 [mac] => a4:3d:78:fc:49:50 [staytime] => 400 ) [1] => Array ( [id] => 20 [receive_id] => 42 [mac] => a4:3d:78:fc:49:51 [staytime] => 200 ) [2] => Array ( [id] => 22 [receive_id] => 42 [mac] => a4:3d:78:fc:49:50 [staytime] => 2443 ) [3] => Array ( [id] => 23 [receive_id] => 42 [mac] => a4:3d:78:fc:49:50 [staytime] => 1443 ) [4] => Array ( [id] => 24 [receive_id] => 42 [mac] => a4:3d:78:fc:49:51 [staytime] => 1143 ) [5] => Array ( [id] => 25 [receive_id] => 42 [mac] => a4:3d:78:fc:49:51 [staytime] => 543 ) )
举例将以上数组按mac区分成
Array ( [a4:3d:78:fc:49:50] => Array ( [id] => 19 [receive_id] => 41 [mac] => a4:3d:78:fc:49:50 [staytime] => 400 [timmarr] => Array ( [0] => Array ( [id] => 19 [receive_id] => 41 [mac] => a4:3d:78:fc:49:50 [staytime] => 400 ) [1] => Array ( [id] => 22 [receive_id] => 42 [mac] => a4:3d:78:fc:49:50 [staytime] => 2443 ) [2] => Array ( [id] => 23 [receive_id] => 42 [mac] => a4:3d:78:fc:49:50 [staytime] => 1443 ) ) ) [a4:3d:78:fc:49:51] => Array ( [id] => 20 [receive_id] => 42 [mac] => a4:3d:78:fc:49:51 [staytime] => 200 [timmarr] => Array ( [0] => Array ( [id] => 20 [receive_id] => 42 [mac] => a4:3d:78:fc:49:51 [staytime] => 200 ) [1] => Array ( [id] => 24 [receive_id] => 42 [mac] => a4:3d:78:fc:49:51 [staytime] => 1143 ) [2] => Array ( [id] => 25 [receive_id] => 42 [mac] => a4:3d:78:fc:49:51 [staytime] => 543 ) ) ) )
----------------------
php代码:
$macArr = [];
$macUserArr = [];
for($i=0;$i<$countList;$i++){
if(!in_array($list[$i][‘mac‘],$macArr)){
$macArr[] = $list[$i][‘mac‘];
$macUserArr[$list[$i][‘mac‘]] = $list[$i];
$macUserArr[$list[$i][‘mac‘]][‘timmarr‘][] = $list[$i];
}else{
$macUserArr[$list[$i][‘mac‘]][‘timmarr‘][] = $list[$i];
}
}
少循环次数
以上是关于php数据优化的主要内容,如果未能解决你的问题,请参考以下文章
PHP性能优化学习笔记--PHP周边性能优化--来自慕课网Pangee http://www.imooc.com/learn/205