Php:检查数组是否全部为真

时间:2014-03-28 23:02:31

标签: php arrays

我试图缩短我的代码,我认为使用数组是最好的方法。

这段代码几乎是由许多不同的查询(mysqli)组成的:

$a = $this->db->query("DELETE * FROM ....");
$b = $this->db->query("DELETE * FROM ....");
$c = $this->db->query("DELETE * FROM ....");
$d = $this->db->query("DELETE * FROM ....");
$e = $this->db->query("DELETE * FROM ....");
$f = $this->db->query("DELETE * FROM ....");
$g = $this->db->query("DELETE * FROM ....");
$h = $this->db->query("DELETE * FROM ....");
$i = $this->db->query("DELETE * FROM ....");
$j = $this->db->query("DELETE * FROM ....");
$k = $this->db->query("DELETE * FROM ....");

要检查这些查询是否正确执行,我这样做(因为它们将返回true / false):

if($a && $b && $c && $d && $e && $f && $g && $h && $i && $j && $k){
    //action    
} 

但每次我添加查询时,我都会将其添加到if语句中并且它已经很长时间我不知道我是否包含它...

所以,现在我想用数组来做这件事:

$a[0] = $this->db->query("DELETE * FROM ....");
$a[1] = $this->db->query("DELETE * FROM ....");
$a[2] = $this->db->query("DELETE * FROM ....");
$a[3] = $this->db->query("DELETE * FROM ....");
$a[4] = $this->db->query("DELETE * FROM ....");
$a[5] = $this->db->query("DELETE * FROM ....");
$a[6] = $this->db->query("DELETE * FROM ....");
$a[7] = $this->db->query("DELETE * FROM ....");
$a[8] = $this->db->query("DELETE * FROM ....");
$a[9] = $this->db->query("DELETE * FROM ....");
$a[10] = $this->db->query("DELETE * FROM ....");

if($a === true){
    //ok
}else{
    //notok;
}

但是这段代码(if语句)不起作用...... 我做错了什么?

4 个答案:

答案 0 :(得分:2)

尝试:

$queries=array(
  'DELETE * FROM ....',
  'DELETE * FROM ....',
  'DELETE * FROM ....',
  'DELETE * FROM ....',
);

$is_all_good=true;
foreach($queries as $sql) 
  if(!$this->db->query($sql)) {$is_all_good=false;break;}

echo $is_all_good?'They are all ok.':'They are not all ok.';

如果你的设置允许交易,这个想法也可以与交易合并。

$queries=array(
  'DELETE * FROM ....',
  'DELETE * FROM ....',
  'DELETE * FROM ....',
  'DELETE * FROM ....',
);

$this->db->autocommit(false);

foreach($queries as $sql) $this->db->query($sql); 

echo $this->db->commit()?'They are all ok.':'They are not all ok.';

答案 1 :(得分:1)

您可以使用数组执行此操作,但您也可以将所有查询放入事务中并检查提交它们是否有效:

$mysqli->autocommit(FALSE);

// put your queries here    

if (!$mysqli->commit()) {
    print("Transaction commit failed");
    exit();
}

这样,您还可以确保事务中的所有查询都成功(这是我假设您要查找或处理的内容)。如果提交失败,则根本不执行任何查询(DDL更改语句除外)。

请参阅http://php.net/manual/en/mysqli.commit.php

答案 2 :(得分:0)

尝试在阵列上使用foreach ...

$a[0] = true;
$a[1] = true;
$a[2] = true;
$a[3] = true;
$a[4] = true;
$a[5] = true;

$all_ok=true;
foreach($a as $a_ok)
{
  if(!($a_ok === true))
  {
    $all_ok=false;
    break;
  }
}

答案 3 :(得分:0)

您应该索引查询本身,而不是索引实际的数据库查询结果。执行每个查询,如果它是假的,那么我们就知道它们并非都是真的。

例如。

$queries = array(
    'SELECT * FROM ...',
    'SELECT * FROM ...',
    'SELECT * FROM ...',
    'SELECT * FROM ...'
);

// iterate through the queries
foreach ($queries as $query) {
    // execute this query and check the result to be false
    if (!$this->db->query($query)) {
        // handle "not ok" case
        // ...

        // exit the function
        return;
    }
}

// they were all true, so we can handle the "ok" case
// ...