note 35801 modified in function.fgetcsv by aidan

From: Date: Thu, 01 Jul 2004 01:01:59 +0000
Subject: note 35801 modified in function.fgetcsv by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-72210@lists.php.net to get a copy of this message
A little contribution to make it more easy to use this function when working with a database. I noticed this function doesn't add logical keys to the array, so I made a small function which creates a 2dimensional array with the corresponding keys added to the rows. <?php // this function requires that the first line of your CSV file consists of the keys corresponding to the values of the other lines 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; } ?> --was-- A little contribution to make it more easy to use this function when working with a database. I noticed this function doesn't add logical keys to the array, so I made a small function which creates a 2dimensional array with the corresponding keys added to the rows. // this function requires that the first line of your CSV file consists of the keys corresponding to the values of the other lines 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; } http://php.net/manual/en/function.fgetcsv.php

« previous php.notes (#72210) next »