2011-04-04 62 views
-1

我已經聲明瞭一個方法,它將根據表動態創建一個數據庫查詢字符串,我的代碼就像這樣。數組只能包含一個鍵值對嗎?

public function checkBeforeDelete($table = array(), $key , $value) 
    { 
     $tableCount = count($table); 
     $queryString = array(); 
     for($i=0;$i<$tableCount;$i++) 
     { 
      $queryString[] = "(SELECT COUNT(*) FROM $table[$i] WHERE $key = $value)+"; 
     } 
     /*********************************************************** 
     Convert the array to a string using implode()    
     Remove all commas(,) using str_replace()     
     Remove the last character of string using substr_replace() 
     ***********************************************************/ 
     $queryString = substr_replace(str_replace(',','',implode(',',$queryString)),'',-2); 
     $queryString = 'SELECT ('. $queryString . ') AS sum'; 
     $sth = $this->dbh->prepare($queryString); 
     $sth->execute(); 
     return $sth->fetchColumn() >= 1; 
    } 

有當$表陣列()將僅具有一個例如

$table = array('states'); 

單個值它仍然計數是一個數組機會。如果一個數組只包含一個鍵值對是否可以?

回答

1

是的。

空數組是有效的也是如此,即:

$someVar = array(); 
1

絕對。

測試:

<?php 

$a = array('test'=> 'key'); 

var_dump($a); 


array(1) { 
    ["test"]=> 
    string(3) "key" 
} 

正如你所看到的,$ a是一個類型的數組與1

長度
相關問題