开发者

Can Array contain only a single key value pair?

I have declared a method where it will dynamically create a database query string depending on the table my code is something like this.

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()
        ***********************************************************/
        $开发者_C百科queryString = substr_replace(str_replace(',','',implode(',',$queryString)),'',-2);
        $queryString = 'SELECT ( '. $queryString . ' ) AS sum';
        $sth = $this->dbh->prepare($queryString);
        $sth->execute();
        return $sth->fetchColumn() >= 1;
    }

there are chances when $table array() will have only one single value for example

$table = array('states');

does it still count to be an array. is it alright if an array contains only a single key value pair?


Yes.

Empty arrays are valid too, ie:

$someVar = array();


Absolutely.

Test:

<?php

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

var_dump($a);


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

As you can see, $a is of type array with a length of 1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜