469,275 Members | 1,802 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

CSV into array

Tom
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

Oct 12 '05 #1
7 20442
Tom wrote:
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


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
Oct 12 '05 #2
> > 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.


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.)


Hello

have a look at this function, and see if it helps you

http://uk2.php.net/manual/en/function.fgetcsv.php

Cheers

Mark
Oct 12 '05 #3
Tom wrote:
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.


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
Oct 12 '05 #4
Tom
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

Oct 12 '05 #5
I've encountered a small problem that I can't figure out though.... $myarray = convertCSVtoAssocMArray("sscreen.csv", ",");


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

Oct 12 '05 #6
JDS
On Wed, 12 Oct 2005 01:18:07 -0700, Tom wrote:
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.


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 | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Oct 12 '05 #7
Tom wrote:
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.


$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
Oct 12 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Brian | last post: by
2 posts views Thread by Stormkid | last post: by
8 posts views Thread by vcardillo | last post: by
12 posts views Thread by Sam Collett | last post: by
8 posts views Thread by Mike S. Nowostawsky | last post: by
104 posts views Thread by Leszek | last post: by
7 posts views Thread by Jim Carlock | last post: by
17 posts views Thread by =?Utf-8?B?U2hhcm9u?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.