CSV import and issues when iterating values

Topics: User Forum
May 19, 2010 at 8:01 PM
Edited May 19, 2010 at 8:36 PM

Here is one row in my CSV file:

*M83513/01-BC,,NE,5,Elect

Using this code:

$values = array();
foreach ($objWorksheet->getRowIterator() as $row) {
  $cellIterator = $row->getCellIterator();
  $cellIterator->setIterateOnlyExistingCells(false);
  foreach ($cellIterator as $cell)
  {
    $cellval = $cell->getValue();
    if (!is_numeric($cellval)) {
      array_push($values, '"'.$cellval.'"');
    } else {
      array_push($values, $cellval);
  }
  print_r($values)."<br>";
}

Here is how it is presented:

Array ( [0] => "*M83513/01-BC" [1] => "" [2] => "NE" [3] => 5 [4] => "Elect" [5] => "" [6] => "" )

Where do the last two elements come from?

Also, if I try to pop the last two values off the array like this:

array_pop($values);
array_pop($values);

the array is now empty. HUH????

Coordinator
May 19, 2010 at 11:05 PM

Array ( [0] => "*M83513/01-BC" [1] => "" [2] => "NE" [3] => 5 [4] => "Elect" [5] => "" [6] => "" )

Where do the last two elements come from?

Is it possible that the CSV has a couple of blank entries in the last two columns of that row.

Also, if I try to pop the last two values off the array like this:

array_pop($values);
array_pop($values);

the array is now empty. HUH????

I have absolutely no idea about this. It's a standard PHP array, so I'd expect array_pop() to simply remove the last element.

May 20, 2010 at 3:44 AM
MarkBaker wrote:

Array ( [0] => "*M83513/01-BC" [1] => "" [2] => "NE" [3] => 5 [4] => "Elect" [5] => "" [6] => "" )

Where do the last two elements come from?

Is it possible that the CSV has a couple of blank entries in the last two columns of that row.

NO, the text you see posted is the entire line. When I open the file in a text editor with no wrapping and place my cursor on the line and hit the end key, it goes right to the last character shown in Elect

 

Also, if I try to pop the last two values off the array like this:

array_pop($values);
array_pop($values);

the array is now empty. HUH????

I have absolutely no idea about this. It's a standard PHP array, so I'd expect array_pop() to simply remove the last element.

Me, neither...