Forum Moderators: coopster

Message Too Old, No Replies

array help

         

Shadowsil

6:12 pm on Jan 27, 2004 (gmt 0)

10+ Year Member



ok so my problem is this. I have a .csv file that I can put into an array..each array has 21 parts of data that I need to define with variables. after that I have to stick certain variables into a mysql database (which I already know how to do) can anyone help me break this array down? here is my code to turn it into an array (sorry Im a noob)
<?php
//set file to read

$file = "c:test.csv";

//put file into an array

$data= file($file) or die("could not read!");

for ($i = 0; $i < count($data); $i++)
{
echo("$data[$i] <BR>\n");
}
//print file contents
echo "all done.";
?>
thank you for all the help :)

dcrombie

6:18 pm on Jan 27, 2004 (gmt 0)



I think you need to use fgetcsv [php.net] to parse the file, then:

foreach ($array_of_file as $row) {
list ($var1, $var2, $var3, ..., $var21) = each ($row);
}

Shadowsil

6:48 pm on Jan 27, 2004 (gmt 0)

10+ Year Member



Im trying to use that but Im getting a parsing error

Warning: fgetcsv(): supplied argument is not a valid stream resource in c:\program files\apache group\apache\htdocs\array2.php on line 8

$file = fgetcsv($data, 4094, ",");

thats correct isnt it? assuming $file is valid.

Timotheos

7:30 pm on Jan 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Take a closer look at the example in the php manual. See how it's using fopen to get the handle and then feeds that into fgetcsv.


<?php
$row = 1;
$handle = fopen("test.csv", "r");
while ($data = fgetcsv($handle, 1000, ",")) {
$num = count($data);
echo "<p> $num fields in line $row: <br />\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
?>