最近写的php函数

返回一个数组,包含最小值与最大值之间的随机数,每个数之间的距离不小于指定值


function randwithgap($min, $max, $num, $gap){
 $arr = array();
 if($max > ($min + $num * ($gap + 1))){
   $maxtime = $num * 10; //最大循环次数,防止卡住
   $i = 0;
   while(count($arr) < $num && $i < $maxtime){
     $temp = mt_rand($min, $max);
     if(count($arr)){
       $ispush = true;
       foreach($arr as $a){
         if(abs($temp - $a) < $gap){
           $ispush = false;
           break;
         }
       }
       if($ispush){
         array_push($arr, $temp);
       }
     }else{
       array_push($arr, $temp);
     }
   }
 }
 return $arr;
}

将数组转换为sql语句

function sqls($where, $font = ' AND ') {
	if (is_array($where)) {
		$sql = '';
		foreach ($where as $key=>$val) {
			$key = str_replace('#', '', $key);
			if(preg_match('/^\[(.*)\]/', $val, $match)){
		      	$val = str_replace($match[0], '', $val);
		      	$op = $match[1];
		    }else{
		      	$op = '=';
		    }
			if(strpos($key,'.')){
				$sql .= $sql ? " $font $key $op '$val' " : " $key $op '$val'";
			}else{
				$sql .= $sql ? " $font `$key` $op '$val' " : " `$key` $op '$val'";
			}
		}
		return $sql;
	} else {
		return $where;
	}
}
sqls(array('id' => 10)) ==>  `id` = 10
sqls(array('id => '[>]10')) ==> `id` > 10
sqls(array('id' => '[>]10', '#id' => '[<]20')) ==> `id` > 10 AND `id` < 20