码迷,mamicode.com
首页 > Web开发 > 详细

php 常见面试题(3)

时间:2017-08-30 13:08:22      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:fun   private   线性   return   size   key   code   dell   close   

1、数组$a = array(‘a‘=>‘a‘,‘b‘=>‘b‘,‘c‘=>‘c‘),如何将array(‘d’=>‘d‘)快速插入 a 和 b 之间?

function wpjam_array_push($array, $data=null, $key=false){
    $data  = (array)$data;
 
    $offset  = ($key===false)?false:array_search($key, array_keys($array));
    $offset  = ($offset)?$offset:false;
    if($offset){
        return array_merge(
            array_slice($array, 0, $offset),
            $data,
            array_slice($array, $offset)
        );
    }else{  // 没指定 $key 或者找不到,就直接加到末尾
        return array_merge($array, $data);
    }
}

$data = array("d"=>d);
$b = wpjam_array_push($a,$data,"b");
print_r($b);

 

2、写一个遍历目录下所有文件以及子目录的函数

function my_scandir($dir)  
{  
    $files = array();  
    if ( $handle = opendir($dir) ) { 
        while ( ($file = readdir($handle)) !== false ) {  
            if ( $file != ".." && $file != "." ) {  
                if ( is_dir($dir . "/" . $file) ) {  
                    $files[$file] = scandir($dir . "/" . $file);  
                }else {  
                    $files[] = $file;  
                }  
            }  
        }  
        closedir($handle);  
        return $files;  
    }  
}

$files=my_scandir(E:\PhpStudy\WWW\12_twleve_month\8-2);
echo "<pre>";
print_r($files); 

 

3、用php实现一个双向队列。

  队列是一种线性表,按照先进先出的原则进行

  单向队列:只能从头进,从尾出

  双向队列:头尾都可以进出

 

class DuiLie {

    private $array = array();//声明空数组
 
    public function setFirst($item){

        return array_unshift($this->array,$item);//头入列
    }

    public function delFirst(){

        return array_shift($this->array);//头出列
    }
 
    public function setLast($item){ 

        return array_push($this->array,$item);//尾入列
    }

    public function delLast(){

        return array_pop($this->array,$item);//尾出列
    }

    public function show(){

        var_dump($this->array);//打印数组
    }

    public function Del(){

        unset($this->array);//清空数组
    }
}   

 

php 常见面试题(3)

标签:fun   private   线性   return   size   key   code   dell   close   

原文地址:http://www.cnblogs.com/lpblogs/p/7452342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!