Read CSV and save to array with variable

2

I have a code where I read the EXCEL .CSV file and display the data on the screen, but I need to save those values in an array.

My code:

<?php
$file = fopen(numeros.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE)
{
  //$line is an array of the csv elements
  print_r($line);
}
fclose($file);
?>

$arrayItem= array('8532300022', '8501022934', '8501022969', '8501022926', '8501022985'); 

It would have to stay something like:

$arrayItem= array('$line');

Valew

    
asked by anonymous 08.11.2015 / 14:58

1 answer

3

Try this:

$meuArray = Array();
$file = fopen('numeros.csv', 'r');
while (($line = fgetcsv($file)) !== false)
{
  $meuArray[] = $line;
}
fclose($file);
print_r($meuArray);

And to use the values of $meuArrray , just use a for or a foreach or simply set an index on variable:

for($i = 0; $i < count($meuArray); $i++){
echo $meuArray[$i];
}

or

foreach($meuArray as $linha => $valor){
echo 'linha '.$linha.' = '.$valor;
}
08.11.2015 / 15:09