note 115776 added to function.str-getcsv
| From: | dejiakala at gmail dot com | Date: | Sun, 21 Sep 2014 06:06:15 +0000 |
| Subject: | note 115776 added to function.str-getcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-200496@lists.php.net to get a copy of this message | ||
I wanted the best of the 2 solutions by james at moss dot io and Jay Williams (csv_to_array()) -
create associative array from a CSV file with a header row.
<?php
$array = array_map('str_getcsv', file('data.csv'));
$header = array_shift($array);
array_walk($array, '_combine_array', $header);
function _combine_array(&$row, $key, $header) {
$row = array_combine($header, $row);
}
?>
Then I thought why not try some benchmarking? I grabbed a sample CSV file with 50,000 rows (10
columns each) and Vulcan Logic Disassembler (VLD) which hooks into the Zend Engine and dumps all the
opcodes (execution units) of a script - see http://pecl.php.net/package/vld and example here: http://fabien.potencier.org/article/8/print-vs-echo-which-one-is-faster
Result:
array_walk() and array_map() - 39 opcodes
csv_to_array() - 69 opcodes
----
Server IP: 87.124.126.49
Probable Submitter: 81.108.40.42
----
Manual Page -- http://php.net/manual/en/function.str-getcsv.php
Edit -- https://master.php.net/note/edit/115776
Del: integrated -- https://master.php.net/note/delete/115776/integrated
Del: useless -- https://master.php.net/note/delete/115776/useless
Del: bad code -- https://master.php.net/note/delete/115776/bad+code
Del: spam -- https://master.php.net/note/delete/115776/spam
Del: non-english -- https://master.php.net/note/delete/115776/non-english
Del: in docs -- https://master.php.net/note/delete/115776/in+docs
Del: other reasons-- https://master.php.net/note/delete/115776
Reject -- https://master.php.net/note/reject/115776
Search -- https://master.php.net/manage/user-notes.php