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


echo count & echo sizeof showing wrong count

我有这个查询

$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)

这里怎么了?

这就是文档所说的:

array_shift()将数组的第一个值偏移并返回。

它不修改数组,但返回修改后的数组。您需要保存该变量,然后对该变量进行计数。

或者你可以这样做:

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