php 妙用陣列鍵值對實現固定資料的查詢
阿新 • • 發佈:2019-02-20
<?php
$walk = new Walk;
$walk->all();
$walk->selectAll();
class Walk{
//陣列鍵值對
private $hrefs = array('aaaa','bbbb','ccccc');
private $names = array('我','走','了');
//根據條件查詢資訊
public function selectAll(){
$dblist = $this->hrefs;
$name = 'b';
if(empty($name)){
die('sdfasdf');
}
$datalist = array();
foreach($dblist as $key=>$db){
$count = 0;
switch($db){
case 'aaaa':
case 'bbbb':
$count = 2;
break;
case 'ccccc':
//查詢資料庫
$count = 0;
break;
default:
$where = '';
break;
}
if($count > 0){
$link_array = $this->build_array($this->names[$key] , $db);
$link_array['post']['name'] = $name;
array_push($datalist , $link_array);
}
}
die(json_encode($datalist , JSON_UNESCAPED_UNICODE));
}
//獲取全部資訊
public function all(){
$list = array_map(array($this , 'build_array') , $this->names , $this->hrefs);
print_r($list);
}
private function build_array($name , $href = ''){
if(!empty($href)){
$href = 'http://www.baidu.com/'.$href;
}
return array('name'=>$name , 'href'=> $href);
}
}
?>
$walk = new Walk;
$walk->all();
$walk->selectAll();
class Walk{
//陣列鍵值對
private $hrefs = array('aaaa','bbbb','ccccc');
private $names = array('我','走','了');
//根據條件查詢資訊
public function selectAll(){
$dblist = $this->hrefs;
$name = 'b';
if(empty($name)){
die('sdfasdf');
}
$datalist = array();
foreach($dblist as $key=>$db){
$count = 0;
switch($db){
case 'aaaa':
case 'bbbb':
$count = 2;
break;
case 'ccccc':
//查詢資料庫
$count = 0;
break;
default:
$where = '';
break;
}
if($count > 0){
$link_array = $this->build_array($this->names[$key] , $db);
$link_array['post']['name'] = $name;
array_push($datalist , $link_array);
}
}
die(json_encode($datalist , JSON_UNESCAPED_UNICODE));
}
//獲取全部資訊
public function all(){
$list = array_map(array($this , 'build_array') , $this->names , $this->hrefs);
print_r($list);
}
private function build_array($name , $href = ''){
if(!empty($href)){
$href = 'http://www.baidu.com/'.$href;
}
return array('name'=>$name , 'href'=> $href);
}
}
?>