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