php 常見面試題(3)

來源:互聯網
上載者:User

標籤: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)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.