回声计数& echo sizeof显示错误计数

时间:2014-04-15 10:56:05

标签: php arrays

我有这个查询

$status = 'failed';
$allTest = $conn->prepare('SELECT SUM( IF( STATUS = :status, 1, 0 ) ) FROM tooldata WHERE testCase REGEXP :var GROUP BY family ORDER BY family' );
$allTest->execute(array(':status' => $status, ':var' => "^$var"));

while($row = $allTest->fetch(PDO::FETCH_ASSOC))
    { 
    foreach($row as $key) 
        {
            $totalTestFailed[] = $key;
        } 
    }

if( (null == $totalTestFailed[0]) || (0 == $totalTestFailed[0]) )
    {
        array_shift($totalTestFailed);
    }

array_shift($totalTestFailed)的输出是

Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 0 [4] => 1 [5] => 1 [6] => 2 [7] => 0 [8] => 0 [9] => 2 [10] => 1 [11] => 1 [12] => 0 [13] => 0 [14] => 0 [15] => 0 [16] => 4 [17] => 0 [18] => 0 [19] => 0 [20] => 20 )

现在当我echo sizeof($totalTestFailed);时,我得到 22 而不是 21

而对于var_dump,我得到数组(大小= 21)

这里有什么问题?

1 个答案:

答案 0 :(得分:0)

这就是文档所说的:

  

array_shift()关闭数组的第一个值并返回它。

它不会修改数组但返回已修改的数组。 您需要保存该变量,然后计算该变量。

或者你可以这样做:

$totalTestFailed = array_shift($totalTestFailed);
echo sizeof($totalTestFailed);