$val["id"], "parent_id" => $parentid); $array[] = $indata; if (isset($val["children"])) { $children = setlist($val["children"], $val["id"]); if ($children) { $array = array_merge($array, $children); } } } return $array; } //初始化树形结构 function infiniteTree($data, $pid = 0) { if (!is_array($data) || empty($data)) return false; $tree = array(); foreach ($data as $k => $v) { // 找出所有的儿子 if ($v['parent_id'] == $pid) { // 将儿子数据赋值给sub键,递归看看儿子还有没有孙子 $v['children'] = infiniteTree($data, $v['id']); $tree[] = $v; // 删除遍历过的数组数据 unset($data[$k]); } } return $tree; } //对象转数组 function object_array($array) { if (is_object($array)) { $array = (array)$array; } if (is_array($array)) { foreach ($array as $key => $value) { $array[$key] = object_array($value); } } return $array; } //求地图中心点 function GetCenterFromDegrees($data) { if (!is_array($data)) return FALSE; $num_coords = count($data); $X = 0.0; $Y = 0.0; $Z = 0.0; foreach ($data as $coord) { $lat = deg2rad((float)$coord['lat']); $lon = deg2rad((float)$coord['lng']); $a = cos($lat) * cos($lon); $b = cos($lat) * sin($lon); $c = sin($lat); $X += $a; $Y += $b; $Z += $c; } $X /= $num_coords; $Y /= $num_coords; $Z /= $num_coords; $lon = atan2($Y, $X); $hyp = sqrt($X * $X + $Y * $Y); $lat = atan2($Z, $hyp); return array('lat' => rad2deg($lat), 'lng' => rad2deg($lon)); } //计算两点之间坐标距离 单位 米 function GetDistance($lat1, $lng1, $lat2, $lng2) { $radLat1 = $lat1 * (PI / 180); $radLat2 = $lat2 * (PI / 180); $a = $radLat1 - $radLat2; $b = ($lng1 * (PI / 180)) - ($lng2 * (PI / 180)); $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))); $s = $s * EARTH_RADIUS; //$s = round($s * 10000) / 10000; return $s * 1000; } //获取一个多边形中心点 到此多边形个点的最大距离 单位 米 function maxDistance(array $centre, array $fence) { $distanceArr = array(); foreach ($fence as $v) { $distanceArr[] = GetDistance($centre['lat'], $centre['lng'], $v['lat'], $v['lng']); } return max($distanceArr); } // lat转latitude lng转longitude function latToLatitude($str) { if (is_array($str)) { $str = json_encode($str); } $pattern_lat = '/lat/'; $replace_lat = 'latitude'; $str_lat = preg_replace($pattern_lat, $replace_lat, $str); $pattern_lng = '/lng/'; $replace_lng = 'longitude'; $str = preg_replace($pattern_lng, $replace_lng, $str_lat); return $str; } // latitude转lat longitude转lng function latitudeTolat($str) { if (is_array($str)) { $str = json_encode($str); } $pattern_lat = '/latitude/'; $replace_lat = 'lat'; $str_lat = preg_replace($pattern_lat, $replace_lat, $str); $pattern_lng = '/longitude/'; $replace_lng = 'lng'; $str = preg_replace($pattern_lng, $replace_lng, $str_lat); return $str; } //高德地图绘制取精确数据 (前端已处理) function getAccurate(array $arr) { $data = []; foreach ($arr as $v) { $bata = []; $bata['lat'] = $v['P']; $bata['lng'] = $v['O']; $data[] = $bata; } return $data; } // 渲染高德地图, 电子围栏数据转换函数 function toGaodeFenceData($str) { $arr = json_decode($str); $res = []; foreach ($arr as $v) { $arr2 = [$v->longitude, $v->latitude]; $res[] = $arr2; } $res = json_encode($res); return $res; } // 渲染高德, 中心点数据转换函数 function toGaodeCentreData($str) { $arr = json_decode($str); $res = [$arr->longitude, $arr->latitude]; $res = json_encode($res); return $res; } /** * 获取图片的Base64编码(不支持url) * @date 2017-02-20 19:41:22 * * @param $img_file 传入本地图片地址 * * @return string */ function imgToBase64($img_file) { $img_base64 = ''; if (file_exists($img_file)) { $app_img_file = $img_file; // 图片路径 $img_info = getimagesize($app_img_file); // 取得图片的大小,类型等 //echo '
' . print_r($img_info, true) . '

'; $fp = fopen($app_img_file, "r"); // 图片是否可读权限 if ($fp) { $filesize = filesize($app_img_file); $content = fread($fp, $filesize); $file_content = chunk_split(base64_encode($content)); // base64编码 switch ($img_info[2]) { //判读图片类型 case 1: $img_type = "gif"; break; case 2: $img_type = "jpg"; break; case 3: $img_type = "png"; break; } $img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content;//合成图片的base64编码 } fclose($fp); } return $img_base64; //返回图片的base64 } //可以发送get和post的请求方式 function curl_request($url, $method = 'get', $data = null, $https = true) { $headers = ["Content-type: application/json;charset='utf-8'", "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache",]; //1.初识化curl $ch = curl_init($url); //2.根据实际请求需求进行参数封装 //返回数据不直接输出 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //如果是https请求 if ($https === true) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } //如果是post请求 if ($method === 'post') { //开启发送post请求选项 curl_setopt($ch, CURLOPT_POST, true); //发送post的数据 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } //3.发送请求 $result = curl_exec($ch); //4.返回返回值,关闭连接 curl_close($ch); return $result; } function php2js($arr) { return json_encode($arr, true); } function js2php($json) { return json_decode($json, true); } function str2arr($str, $glue = ',') { return explode($glue, $str); } function arr2str($arr, $glue = ',') { return implode($glue, array_wrap($arr)); } function objectToArray($object) { //先编码成json字符串,再解码成数组 return json_decode(json_encode($object), true); } // 是否是序列化字符串 function is_serialized( $data ) { $data = trim( $data ); if ( 'N;' == $data ) return true; if ( !preg_match( '/^([adObis]):/', $data, $badions ) ) return false; switch ( $badions[1] ) { case 'a' : case 'O' : case 's' : if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) ) return true; break; case 'b' : case 'i' : case 'd' : if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) ) return true; break; } return false; } function wechat_fee($money) { return ($money * 100); }