php 代码片段

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,GB
function 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值