note 82905 added to function.fgetcsv
| From: | skirkendall at NOSPAM dot dsl-only dot net | Date: | Thu, 01 May 2008 01:55:46 +0000 |
| Subject: | note 82905 added to function.fgetcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-138955@lists.php.net to get a copy of this message | ||
The array_flip() function is handy for converting column names to column numbers. Assuming the
first row contains column names, you can simply read it via fgetcsv(); this will give you a
number-indexed array of column names. Applying array_flip() converts that into a name-indexed array
of column numbers.
The following example does this, and assumes that two of the columns are named "animal"
and "sound" but does not make any assumption about where those columns are.
$fp = fopen($url, "r");
$names = array_flip(fgetcsv($fp, 1000));
while (($values = fgetcsv($fp, 1000)) !== FALSE) {
print "The ".$values[$names["animal"]]." says
".$values[$names["sound"]].".\n";
}
fclose($fp);
----
Server IP: 208.69.120.35
Probable Submitter: 63.105.22.161
----
Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php
Edit -- https://master.php.net/note/edit/82905
Del: integrated -- https://master.php.net/note/delete/82905/integrated
Del: useless -- https://master.php.net/note/delete/82905/useless
Del: bad code -- https://master.php.net/note/delete/82905/bad+code
Del: spam -- https://master.php.net/note/delete/82905/spam
Del: non-english -- https://master.php.net/note/delete/82905/non-english
Del: in docs -- https://master.php.net/note/delete/82905/in+docs
Del: other reasons-- https://master.php.net/note/delete/82905
Reject -- https://master.php.net/note/reject/82905
Search -- https://master.php.net/manage/user-notes.php