php 代码片段
来源:互联网 发布:java经典算法100例 编辑:程序博客网 时间:2024/05/05 11:38
php 将已定义好的数组的key,改变为从1开始
方法1:
$array = array('a', 'b', 'c'); // defined elsewhere$array = array_filter(array_merge(array(0), $array));//还可以使用 两次 array_flip();进行去重
方法2:
$array = array("a","b","c");array_unshift($array,"");unset($array[0]);
方法3:
for ($a = 0; $a < 1000; ++$a) { $alphabet = array("a", "b", "c"); array_unshift($alphabet, "phoney"); unset($alphabet[0]);}
php 判断与去除数组中重复数据的方法
//判断if (count($array) != count(array_unique($array))) { echo '该数组有重复值'; }//去除$input = array("a" => "green","", "red","b" => "green", "","blue", "red","c" => "witer","hello","witer");//$result = array_unique($input); //去除重复元素$result = a_array_unique($input); //只留下单一元素foreach($result as $aa) { echo $aa."<br />";}function multi_unique($array) { foreach ($array as $k=>$na) { $new[$k] = serialize($na); } $uniq = array_unique($new); foreach($uniq as $k=>$ser) { $new1[$k] = unserialize($ser); } return ($new1);}function a_array_unique($array) {//写的比较好 $out = array(); foreach ($array as $key=>$value) { if (!in_array($value, $out)) { $out[$key] = $value; } } return $out;} //PHP数组去除重复项 有个内置函数array_unique (),但是php的 array_unique函数只适用于一维数组,//对多维数组并不适用。//以下实现一个二维数组的array_unique函数:function unique_arr($array2D,$stkeep=false,$ndformat=true){ // 判断是否保留一级数组键 (一级数组键可以为非数字) if($stkeep) { $stArr = array_keys($array2D); } // 判断是否保留二级数组键 (所有二级数组键必须相同) if($ndformat) { $ndArr = array_keys(end($array2D)); } //降维,也可以用implode,将一维数组转换为用逗号连接的字符串 foreach ($array2D as $v) { $v = join(",",$v); $temp[] = $v; } //去掉重复的字符串,也就是重复的一维数组 $temp = array_unique($temp); //再将拆开的数组重新组装 foreach ($temp as $k => $v) { if($stkeep){ $k = $stArr[$k]; } if($ndformat) { $tempArr = explode(",",$v); foreach($tempArr as $ndkey => $ndval) { $output[$k][$ndArr[$ndkey]] = $ndval; } } else { $output[$k] = explode(",",$v); } } return $output;}//测试$array2D = array( 'first'=>array('title'=>'1111','date'=>'2222'), 'second'=>array('title'=>'1111','date'=>'2222'), 'third'=>array('title'=>'2222','date'=>'3333')); print_r($array2D); print_r(unique_arr($array2D, true));
php 输出 A-Z AA-ZZ
$addon = 64;for ($i = 1; $i <= 700; $i++) { $prefix = ""; if ($i > 26) { $remainder = floor($i / 26); $prefix = chr($remainder + $addon); } $ivalue = ($i % 26 == 0) ? 26 : $i % 26; echo $prefix . chr($addon + $ivalue) . "<br />";}
输出 AA - ZZ
foreach (range('A', 'Z') as $char) { foreach (range('A', 'Z') as $char1) { echo $char . $char1. "\n"; } }
输出 AA - ZY
for($x = 'A'; $x < 'ZZ'; $x++){ echo $x, ' ';}
php 输出文件的大小 kB
//代码也可以用于统计目录数//格式化输出目录大小 单位:Bytes,KB,MB,GBfunction getDirectorySize($path){ $totalsize = 0; $totalcount = 0; $dircount = 0; if ($handle = opendir($path)) { while (false !== ($file = readdir($handle))) { $nextpath = $path . '/' . $file; if ($file != '.' && $file != '..' && !is_link($nextpath)) { if (is_dir($nextpath)) { $dircount++; $result = getDirectorySize($nextpath); $totalsize += $result['size']; $totalcount += $result['count']; $dircount += $result['dircount']; } elseif (is_file($nextpath)) { $totalsize += filesize($nextpath); $totalcount++; } } } } closedir($handle); $total['size'] = $totalsize; $total['count'] = $totalcount; $total['dircount'] = $dircount; return $total;}
public function formatBytes($size, $precision = 2){ if ($size > 0) { $size = (int) $size; $base = log($size) / log(1024); $suffixes = array(' bytes', ' KB', ' MB', ' GB', ' TB'); return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)]; } else { return $size; }}
function calcSize($size, $accuracy = 2){ $units = array('B', 'KB', 'MB', 'GB'); foreach ($units as $n => $u) { $div = pow(1024, $n); if ($size > $div) $output = number_format($size / $div, $accuracy) . $u; } return $output;}
function getNiceFileSize($file, $digits = 2){ if (is_file($file)) { $filePath = $file; if (!realpath($filePath)) { $filePath = $_SERVER["DOCUMENT_ROOT"] . $filePath; } $fileSize = filesize($filePath); $sizes = array("TB", "GB", "MB", "KB", "B"); $total = count($sizes); while ($total-- && $fileSize > 1024) { $fileSize /= 1024; } return round($fileSize, $digits) . " " . $sizes[$total]; } return false;}
$units = explode(' ','B KB MB GB TB PB');echo("<html><body>");echo('file size: ' . format_size(filesize("example.txt")));echo("</body></html>");function format_size($size){ $mod = 1024; for ($i = 0; $size > $mod; $i++) { $size /= $mod; } $endIndex = strpos($size, ".")+3; return substr( $size, 0, $endIndex).' '.$units[$i];}
function size2Byte($size){ $units = array('KB', 'MB', 'GB', 'TB'); $currUnit = ''; while (count($units) > 0 && $size > 1024) { $currUnit = array_shift($units); $size /= 1024; } return ($size | 0) . $currUnit;}
public function sizeFilter( $bytes ){ $label = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB' ); for( $i = 0; $bytes >= 1024 && $i < ( count( $label ) -1 ); $bytes /= 1024, $i++ ); return( round( $bytes, 2 ) . " " . $label[$i] );}
function filesize_formatted($path){ //filesize() 函数不能判断 大于2G 的文件 $size = filesize($path); $units = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); $power = $size > 0 ? floor(log($size, 1024)) : 0; return number_format($size / pow(1024, $power), 2, '.', ',') . ' ' . $units[$power];}
function Size($path){ $bytes = sprintf('%u', filesize($path)); if ($bytes > 0) { $unit = intval(log($bytes, 1024)); $units = array('B', 'KB', 'MB', 'GB'); if (array_key_exists($unit, $units) === true) { return sprintf('%d %s', $bytes / pow(1024, $unit), $units[$unit]); } } return $bytes;}
这个方法用于将秒数转换成年,月,周,日等。
function Sec2Time($time){ if (is_numeric($time)) { $value = [ "years" => 0, "days" => 0, "hours" => 0, "minutes" => 0, "seconds" => 0, ]; if ($time >= 31556926) { $value["years"] = floor($time / 31556926); $time = ($time % 31556926); } if ($time >= 86400) { $value["days"] = floor($time / 86400); $time = ($time % 86400); } if ($time >= 3600) { $value["hours"] = floor($time / 3600); $time = ($time % 3600); } if ($time >= 60) { $value["minutes"] = floor($time / 60); $time = ($time % 60); } $value["seconds"] = floor($time); return (array)$value; } else { return (bool)false; }}echo '<pre>';print_r(Sec2Time(5));
0 0
- php代码片段
- PHP-常用代码片段
- PHP代码片段
- PHP 常用代码片段
- PHP常用代码片段
- php 代码片段
- [php] 代码片段
- php 代码片段
- 【代码片段】php分页代码
- 10个PHP代码片段
- 实用的PHP代码片段
- PHP 常用的代码片段
- 一些实用PHP代码片段
- PHP之代码片段收集
- php生成印章 代码片段
- 10 个有用的 PHP 代码片段
- 10 个救命的 PHP 代码片段
- 10 个救命的 PHP 代码片段
- IOS策略模式与多态
- Android图片大小处理
- Cookies 和 Session的区别
- Opendap 4:Hyrax配置BES netcdf handler
- Volley请求
- php 代码片段
- linux不能ping通域名能ping通ip
- OJ解题报告 4976:硬币
- eclipse下安装pydev解决方案
- hdu 3729 I'm Telling the Truth【二分匹配】
- 数据结构与算法究竟重要吗?
- 关于随机数的学习
- 通过Sqoop实现Mysql / Oracle 与HDFS / Hbase互导数据
- Python(Django) Restful Vue developent flow chart