在项目开发中经常用到的全局函数

//打印

function p($res,$exit = 0){

    echo '<pre>';

    print_r($res);

    echo '</pre>';

    if($exit){

        exit();

    }

}

// 是否正整数

function is_positive_integer($num = 0) {  

return is_numeric($num) && $num == (int) $num && $num > 0;

}

// 是否非空字符串

function is_not_empty_string($str = '') {

return is_string($str) && $str != '';

}

// 是否非空数组

function is_not_empty_array($arr = array()) {

return is_array($arr) && !empty($arr);

}

/** 

* 计算两组经纬度坐标 之间的距离 

* params :lat1 纬度1; lng1 经度1; lat2 纬度2; lng2 经度2; len_type (1:m or 2:km); 

* return int m or km 

*/ 

function getDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2) {

$EARTH_RADIUS = 6378.137;

$PI = 3.1415926;

$radLat1 = $lat1 * $PI / 180.0;

$radLat2 = $lat2 * $PI / 180.0;

$a = $radLat1 - $radLat2;

$b = ($lng1 * $PI / 180.0) - ($lng2 * $PI / 180.0);

$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 * 1000);

if ($len_type > 1)

{

$s /= 1000;

}

return round($s, $decimal);

}

//是否是微信端

function is_weixin(){ 

   if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {

      return true;

   }else{

      return false;

   }

       

}

//合并两个二维索引数组

function mergeByCId(&$a,&$b){

$c=array();

foreach($a as $e) $c[$e['cid']]=$e;

foreach($b as $e) $c[$e['cid']]=isset($c[$e['cid']])? $c[$e['cid']]+$e : $e;

return $c;

}

原文地址:https://www.cnblogs.com/yszr/p/8120378.html