php基本函数汇总(2)

$arr = array( array('id'=>'2','title'=>'...','ding'=>'1','jing'=>'1','time'=>'...','url'=>'...','dj'=>'...'), array('id'=>'2','title'=>'...','ding'=>'1','jing'=>'1','time'=>'...','url'=>'...','dj'=>'...') ); function about_unique($arr=array()){ /*将该种二维数组看成一维数组,则 该一维数组的value值有相同的则干掉只留一个,并将该一维 数组用重排后的索引数组返回,而返回的一维数组中的每个元素都是 原始key值形成的关联数组 */ $keys =array(); $temp = array(); foreach($arr[0] as $k=>$arrays) { /*数组记录下关联数组的key值*/ $keys[] = $k; } //return $keys; /*降维*/ foreach($arr as $k=>$v) { $v = join(",",$v); //降维 $temp[] = $v; } $temp = array_unique($temp); //去掉重复的内容 foreach ($temp as $k => $v){ /*再将拆开的数组按索引数组重新组装*/ $temp[$k] = explode(",",$v); } //return $temp; /*再将拆开的数组按关联数组key值重新组装*/ foreach($temp as $k=>$v) { foreach($v as $kkk=>$ck) { $data[$k][$keys[$kkk]] = $temp[$k][$kkk]; } } return $data; }

13.格式化字节大小

/** * 格式化字节大小 * @param number $size 字节数 * @param string $delimiter 数字和单位分隔符 * @return string 格式化后的带单位的大小 * @author */ function format_bytes($size, $delimiter = '') { $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB'); for ($i = 0; $size >= 1024 && $i < 6; $i++) $size /= 1024; return round($size, 2) . $delimiter . $units[$i]; }

14.3分钟前

/** * 将指定时间戳转换为截止当前的xx时间前的格式 例如 return '3分钟前'' * @param string|int $timestamp unix时间戳 * @return string */ function time_ago($timestamp) { $etime = time() - $timestamp; if ($etime < 1) return '刚刚'; $interval = array ( 12 * 30 * 24 * 60 * 60 => '年前 ('.date('Y-m-d', $timestamp).')', 30 * 24 * 60 * 60 => '个月前 ('.date('m-d', $timestamp).')', 7 * 24 * 60 * 60 => '周前 ('.date('m-d', $timestamp).')', 24 * 60 * 60 => '天前', 60 * 60 => '小时前', 60 => '分钟前', 1 => '秒前' ); foreach ($interval as $secs => $str) { $d = $etime / $secs; if ($d >= 1) { $r = round($d); return $r . $str; } }; }

15.身份证号

/** * 判断参数字符串是否为天朝身份证号 * @param $id 需要被判断的字符串或数字 * @return mixed false 或 array[有内容的array boolean为真] */ function is_citizen_id($id) { //长度效验 18位身份证中的X为大写 $id = strtoupper($id); if(!(preg_match('/^\d{17}(\d|X)$/',$id) || preg_match('/^\d{15}$/',$id))) { return false; } //15位老号码转换为18位 并转换成字符串 $Wi = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1); $Ai = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); $cardNoSum = 0; if(strlen($id)==16) { $id = substr(0, 6).'19'.substr(6, 9); for($i = 0; $i < 17; $i++) { $cardNoSum += substr($id,$i,1) * $Wi[$i]; } $seq = $cardNoSum % 11; $id = $id.$Ai[$seq]; } //效验18位身份证最后一位字符的合法性 $cardNoSum = 0; $id17 = substr($id,0,17); $lastString = substr($id,17,1); for($i = 0; $i < 17; $i++) { $cardNoSum += substr($id,$i,1) * $Wi[$i]; } $seq = $cardNoSum % 11; $realString = $Ai[$seq]; if($lastString!=$realString) {return false;} //地域效验 $oCity = array(11=>"北京",12=>"天津",13=>"河北",14=>"山西",15=>"内蒙古",21=>"辽宁",22=>"吉林",23=>"黑龙江",31=>"上海",32=>"江苏",33=>"浙江",34=>"安徽",35=>"福建",36=>"江西",37=>"山东",41=>"河南",42=>"湖北",43=>"湖南",44=>"广东",45=>"广西",46=>"海南",50=>"重庆",51=>"四川",52=>"贵州",53=>"云南",54=>"西藏",61=>"陕西",62=>"甘肃",63=>"青海",64=>"宁夏",65=>"新疆",71=>"台湾",81=>"香港",82=>"澳门",91=>"国外"); $City = substr($id, 0, 2); $BirthYear = substr($id, 6, 4); $BirthMonth = substr($id, 10, 2); $BirthDay = substr($id, 12, 2); $Sex = substr($id, 16,1) % 2 ;//男1 女0 //$Sexcn = $Sex?'男':'女'; //地域验证 if(is_null($oCity[$City])) {return false;} //出生日期效验 if($BirthYear>2078 || $BirthYear<1900) {return false;} $RealDate = strtotime($BirthYear.'-'.$BirthMonth.'-'.$BirthDay); if(date('Y',$RealDate)!=$BirthYear || date('m',$RealDate)!=$BirthMonth || date('d',$RealDate)!=$BirthDay) { return false; } return array('id'=>$id,'location'=>$oCity[$City],'Y'=>$BirthYear,'m'=>$BirthMonth,'d'=>$BirthDay,'sex'=>$Sex); }

16.获取二维数组中某个key的集合

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/b4f1196498c40fbcbb0a44836fe91295.html