|
Posted by Ewoud Dronkert on 10/12/05 22:07
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
[Back to original message]
|