Malloc
Malloc

Reputation: 16276

count returning the size of the first cell in the array and not the size of the entire array

I have an array $all_orders which contains arrays, the $temp_array contains every iteration after explode a string composed of 8 words, and $handle is composed of 3 lines in the file:

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

Now when i try to count the $all_orders array elements (which is supposed to be 3), i get simply 8:

echo count($all_orders);// display 8

why didn't i get 3 ?

EDIT:

Here is the result i got when trying to print to content of the array:

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] => [email protected]
            [3] => chaine.
            [4] => chaine
            [5] => chaine
            [6] => chaine
            [7] => chaine

        )

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

        )

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

        )

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

)

Upvotes: 0

Views: 70

Answers (1)

DaveRandom
DaveRandom

Reputation: 88647

You have one of two problems:

  • Your file has some blanks lines in it
  • PHP is getting confused by the style of the line endings (such as \r\n on a platform that just uses \n)

Regardless of what the problem actually is, this should fix it:

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);
}

Upvotes: 1

Related Questions