Php 数组只能包含一个键值对吗?
我声明了一个方法,它将根据表动态创建数据库查询字符串。我的代码是这样的Php 数组只能包含一个键值对吗?,php,Php,我声明了一个方法,它将根据表动态创建数据库查询字符串。我的代码是这样的 public function checkBeforeDelete($table = array(), $key , $value ) { $tableCount = count($table); $queryString = array(); for($i=0;$i<$tableCount;$i++) { $querySt
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;
}
它仍然算是一个数组吗。如果数组只包含一个键值对,可以吗?可以
空数组也有效,即:
$someVar = array();
当然
测试:
正如您所看到的,$a是长度为1的数组类型
$someVar = array();
<?php
$a = array('test'=> 'key');
var_dump($a);
array(1) {
["test"]=>
string(3) "key"
}