I would like the following code to assign a variable to each value read from my CSV file.
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>
So for example, inside the CSV file I have 4 values from 2 Excel columns:
Jack,31/01/1991
Ross,15/03/1989
And I would like the make Jack variable $vrble1, 31/01/1991 $vrble2, Ross $vrble3 and 15/03/1989 $vrble4.
How can I do that?
Per each iteration you merge the data into a single array:
And then you can extract the array data into the local varialbles like this:
See
array_combineandextract.However you should continue using arrays if you ask me.