首页 > 代码库 > php数据优化

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数据优化