count返回数组中第一个单元格的大小,而不是整个数组的大小

时间:2012-01-08 11:20:37

标签: php

我有一个数组$all_orders,其中包含数组,$ temp_array包含爆炸后由8个单词组成的字符串的每次迭代,$handle3 lines组成文件:

while(!feof($handle))
{
  $order=fgets($handle);
  $temp_array=explode(",",$order);
  array_push($all_orders,$temp_array);
}

现在,当我尝试计算$all_orders数组元素(应该是3)时,我只得到8:

echo count($all_orders);// display 8

为什么我没有得到3?

修改

这是我在尝试打印到数组内容时得到的结果:

Array
(
    [0] => Array
        (
            [0] =>

        )

    [1] => Array
        (
            [0] =>

        )

    [2] => Array
        (
            [0] => 1
            [1] => 1
            [2] => chaine.com
            [3] => chaine
            [4] => chaine
            [5] => chaine
            [6] => chaine
            [7] => chaine

        )

    [3] => Array
        (
            [0] =>

        )

    [4] => Array
        (
            [0] => 2
            [1] => 1
            [2] => chaine@fudd.com
            [3] => chaine.
            [4] => chaine
            [5] => chaine
            [6] => chaine
            [7] => chaine

        )

    [5] => Array
        (
            [0] =>

        )

    [6] => Array
        (
            [0] => 3
            [1] => 2
            [2] => chaine@bunny.com
            [3] => chaine.
            [4] => chaine
            [5] => chaine
            [6] => chaine
            [7] => chaine

        )

    [7] => Array
        (
            [0] =>
        )

)

1 个答案:

答案 0 :(得分:1)

你有两个问题之一:

  • 您的文件中有一些空白行
  • PHP对行结尾的样式感到困惑(例如在\r\n平台上使用\n

无论问题究竟是什么,都应该解决它:

while(!feof($handle)) {
  // Evaluating the trimmed line as a bool will effectively skip blank lines
  if (!trim($order = fgets($handle))) continue;
  $all_orders[] = explode(',', $order);
}