note 112069 added to function.array-flip
| From: | TonyH | Date: | Mon, 29 Apr 2013 14:59:37 +0000 |
| Subject: | note 112069 added to function.array-flip | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-194651@lists.php.net to get a copy of this message | ||
This function is useful when parsing a CSV file with a heading column, but the columns might vary in
order or presence:
<?php
$f = fopen("file.csv", "r");
/* Take the first line (the header) into an array, then flip it
so that the keys are the column name, and values are the
column index. */
$cols = array_flip(fgetcsv($f));
while ($line = fgetcsv($f))
{
// Now we can reference CSV columns like so:
$status = $line[$cols['OrderStatus']];
}
?>
I find this better than referencing the numerical array index.
----
Server IP: 69.147.83.199
Probable Submitter: 217.34.54.3
----
Manual Page -- http://php.net/manual/en/function.array-flip.php
Edit -- https://master.php.net/note/edit/112069
Del: integrated -- https://master.php.net/note/delete/112069/integrated
Del: useless -- https://master.php.net/note/delete/112069/useless
Del: bad code -- https://master.php.net/note/delete/112069/bad+code
Del: spam -- https://master.php.net/note/delete/112069/spam
Del: non-english -- https://master.php.net/note/delete/112069/non-english
Del: in docs -- https://master.php.net/note/delete/112069/in+docs
Del: other reasons-- https://master.php.net/note/delete/112069
Reject -- https://master.php.net/note/reject/112069
Search -- https://master.php.net/manage/user-notes.php