PHP二分查找法

$array = array(11,3,56,62,21,66,32,78,36,76,39,88,34);
$target = 32;
$beg = 0;
$end = count($array) - 1;
// 二分查找算法,指定数字里面的键是多少
function binSearch($array, $beg, $end, $target)
{
    if ($beg <= $end){
        var_dump($beg, $end);echo "\n";
        $mid =  intval(($beg+$end)/2 );
        if ($array[$mid] ==  $target){
            return $mid;
        }elseif ($target <= $array[$mid]){
            return binSearch($array, $beg,  $mid-1, $target);
        }else{
            return binSearch($array,$mid+1, $end-1, $target);
        }
    }
    return false;
}
$find = binSearch($array, $beg, $end, $target);
var_dump($find);

PHP交叉排列的组合算法

应用场景商城类多规格组合如:
['红色','白色','蓝色','黑色']
['S','M','L','XL','XXL']

需要得到能有多少种组合

$combine_array = [['红色','白色','蓝色','黑色'],['S','M','L','XL','XXL']]
// 交叉排列的组合算法
protected function combine($array)
{
    $heads = $array[0];
    for ($i = 1; $i < count($array); $i++){
        if (count($array[$i])){
            $heads = $this->addNewType($heads, $array[$i]);
        }
    }
    return $heads;
}

protected function addNewType($heads, $choices)
{
    $result = [];
    for ($i = 0; $i < count($heads); $i++){
        for ($j = 0; $j < count($choices); $j++){
            $result[] = $heads[$i] . ':' . $choices[$j];
        }
    }
    return $result;
}

php导出数据保存到cvs文存储到目录

设置内存限制

set_time_limit(0);
ini_set('memory_limit', '1024M');

// 设置cvs第一行数据
$header = ['A1','B1','C1','D1'];
$csvList = [
    ['A2','B2','C2','D2'],
    ['A3','B3','C3','D3'],
    ['A4','B4','C4','D4'],
];
//下载csv的文件名
$filename = getcwd() . '/csv/用户骑行数据' . date('YmdHis', $time_range) . '.csv';
exportCsv($data, $title_arr, $file_name = '')
{
    $csv_data = '';
    // 标题
    $num = count($title_arr);
    for ($i = 0; $i < $num - 1; ++$i) {
        $csv_data .= '"' . $title_arr[$i] . '",';
    }
    if ($num > 0) {
        $csv_data .= '"' . $title_arr[$num - 1] . "\"\r\n";
    }
    foreach ($data as $k => $row) {
        for ($i = 0; $i < $num - 1; ++$i) {
            $row[$i] = str_replace("\"", "\"\"", $row[$i]);
            $csv_data .= '"' . $row[$i] . '",';
        }
        $csv_data .= '"' . $row[$num - 1] . "\"\r\n";
        unset($data[$k]);
    }
    $file_name = empty($file_name) ? date('Y-m-d-H-i-s', time()) : $file_name;
    file_put_contents($file_name, $csv_data) ;
}
exportCsv($csvList, $header, $filename);

php获取多边形GPS中心点坐标getGpsCenter

直接帖方法
    $gps_data = [
        [119.182468,34.603796],
        [119.182635,34.603347],
        [119.182743,34.603370],
        [119.182575,34.603846].
    ];
    // 获取GPS中心点坐标
    function getGpsCenter($gps_data = [])
    {
        if (!is_array($gps_data)) return false;
        $num_coords = count($gps_data);

        $X = 0.0;
        $Y = 0.0;
        $Z = 0.0;

        foreach ($gps_data as $val)
        {
            #$lon = $coord[1] * pi() / 180;
            $lng = deg2rad($val[0]);
            #$lat = $coord[0] * pi() / 180;
            $lat = deg2rad($val[1]);

            $a = cos($lat) * cos($lng);
            $b = cos($lat) * sin($lng);
            $c = sin($lat);

            $X += $a;
            $Y += $b;
            $Z += $c;
        }

        $X /= $num_coords;
        $Y /= $num_coords;
        $Z /= $num_coords;

        $lng = atan2($Y, $X);
        $hyp = sqrt($X * $X + $Y * $Y);
        $lat = atan2($Z, $hyp);

        #return array($lat * 180 / pi(), $lon * 180 / pi());
        return array(rad2deg($lng), rad2deg($lat));
    }