CSV into array | | |
PHP Gurus...
Can anyone give me a helping hand with this.
I'm more ASP, but trying to move to PHP and sturggling a bit.
I have a few products stored in a csv file created in Excel. Column 1
is the part #, column 2 is title, column 3 is color, column 4 is width
and 5 is price.
I need to read these into arrays, with a seperate array for each
column.
Can anyone suggest anything?
If someone could point me in the right direction it would be much
appreciated!
Tom | | | | re: CSV into array
Tom wrote:
[color=blue]
> PHP Gurus...
>
> Can anyone give me a helping hand with this.
>
> I'm more ASP, but trying to move to PHP and sturggling a bit.
>
> I have a few products stored in a csv file created in Excel. Column 1
> is the part #, column 2 is title, column 3 is color, column 4 is width
> and 5 is price.
>
> I need to read these into arrays, with a seperate array for each
> column.
>
> Can anyone suggest anything?
>
> If someone could point me in the right direction it would be much
> appreciated!
>
> Tom[/color]
Hi Tom,
Try something like this:
1) find out what the end-of-line is. Probably \n
2) read the csv into an array using file().
Now each arrayelement is a row from the csv.
check www.php.net for details.
3) Explode each line, using the seperator for columns. (can be tab \t or ,
or whatever you decided.)
Good luck.
Regards,
Erwin Moller | | | | re: CSV into array
> > I have a few products stored in a csv file created in Excel. Column 1[color=blue][color=green]
> > is the part #, column 2 is title, column 3 is color, column 4 is width
> > and 5 is price.
> >
> > I need to read these into arrays, with a seperate array for each
> > column.
> >[/color]
>
> Try something like this:
> 1) find out what the end-of-line is. Probably \n
> 2) read the csv into an array using file().
> Now each arrayelement is a row from the csv.
> check www.php.net for details.
>
> 3) Explode each line, using the seperator for columns. (can be tab \t or ,
> or whatever you decided.)[/color]
Hello
have a look at this function, and see if it helps you http://uk2.php.net/manual/en/function.fgetcsv.php
Cheers
Mark | | | | re: CSV into array
Tom wrote:
[color=blue]
> I have a few products stored in a csv file created in Excel. Column 1
> is the part #, column 2 is title, column 3 is color, column 4 is width
> and 5 is price.
>
> I need to read these into arrays, with a seperate array for each
> column.[/color]
Try something like:
<?php
$lines =file('whatever.csv');
foreach($lines as $line)
{
list($partno[],$title[],$color[], ... )
= explode(',',$line);
}
?>
Keep in mind that all CSV files are not equal: some use other delimiter than
comma for separating fields, some encapsulate fields within double quotes,
etc. The above code is for a trivial CSV file.
--
----------------------------------
Iván Sánchez Ortega -ivansanchez-arroba-escomposlinux-punto-org
TAG no encontrado... Insert disk #2 | | | | re: CSV into array
Great, thanks for the tips. I've managed to overcome the hard bit now
:)
I've encountered a small problem that I can't figure out though....
My 1st page is a table with each of the products listed, with a
quantity form field for each product. The name of this field is qty1,
qty2 etc depending on which product it is. With me so far I hope....
The next page calls up the CSV again and loops through each line. I now
want to request the quantity form field for each product.
So I'm trying this
function convertCSVtoAssocMArray($file, $delimiter)
{
$result = Array();
$size = filesize($file) +1;
$file = fopen($file, 'r');
$keys = fgetcsv($file, $size, $delimiter);
while ($row = fgetcsv($file, $size, $delimiter))
{
for($i = 0; $i < count($row); $i++)
{
if(array_key_exists($i, $keys))
{
$row[$keys[$i]] = $row[$i];
}
}
$result[] = $row;
}
fclose($file);
return $result;
}
$myarray = convertCSVtoAssocMArray("sscreen.csv", ",");
$numElements = count($myarray);
for($counter=0; $counter < $numElements; $counter++)
{
$unitprice = $qty * $myarray[$counter][4];
echo $qty[$counter];
echo $counter;
}
But nothing is outputting! Any ideas? I basically need to construct the
$qty1 variable by adding the record count onto the end.
Again, thanks for all your help!
Tom | | | | re: CSV into array
[color=blue]
> I've encountered a small problem that I can't figure out though....[/color]
[color=blue]
> $myarray = convertCSVtoAssocMArray("sscreen.csv", ",");[/color]
Try adding this line after the one quoted above:
print_r( $myarray );
and see if what you get in the array matches your expectations.
---
Steve | | | | re: CSV into array
On Wed, 12 Oct 2005 01:18:07 -0700, Tom wrote:
[color=blue]
> I have a few products stored in a csv file created in Excel. Column 1
> is the part #, column 2 is title, column 3 is color, column 4 is width
> and 5 is price.[/color]
example of how I woud do it: (WARNING: Untested code!)
<?php
$file = file("theFile.csv");
$r=0;
foreach ($file as $line){
list($row[$r]['partno'], $row[$r]['title'], $row[$r]['color'],
$row[$r]['width'], $row[$r]['price']) = split(",", $line);
}
?>
That puts the whole file into a 2-dimensional arra ($row) that simulates
the layout of the Excel sheet. Do whatever you wnat with it after that.
--
JDS | jeffrey@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/ | | | | re: CSV into array
Tom wrote:[color=blue]
> I have a few products stored in a csv file created in Excel. Column 1
> is the part #, column 2 is title, column 3 is color, column 4 is width
> and 5 is price.
>
> I need to read these into arrays, with a seperate array for each
> column.[/color]
$maxlinelength = 1000;
$fh = fopen('inventory.csv', 'r');
$firstline = fgetcsv($fh, $maxlinelength);
$cols = count($firstline);
$row = 0;
$inventory = array();
while ( ($nextline = fgetcsv($fh, $maxlinelength)) !== FALSE )
{
for ( $i = 0; $i < $cols; ++$i )
{
$inventory[$firstline[$i]][$row] = $nextline[$i];
}
++$row;
}
fclose($fh);
That puts the data in the array $inventory where $inventory['partno'][10]
is the 11th item in the 'partno' column. Column headings from the csv file
are used as keys for the associative array $inventory. You may address the
columns as "separate arrays" like so: $inventory['partno'] which is in
itself a numerically indexed array with 0 <= index < $row.
--
E. Dronkert |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|