Read CSV file into an array

Uses fgetcsv() to read CSV into associative array. Read more.

php
fork add to bundle source
function csv_to_array($file) { if ( !$handle = fopen($file, 'r') ) return; while (($row = fgetcsv($handle)) !== false) { if ( !$head ) { $head = $row; continue; } $entity = []; foreach ( $head as $index => $column ) { $entity[$column] = $row[$index]; } $data[] = $entity; } return $data; }

usage

load_function('csv_to_array'); $data = csv_to_array('example.csv'); print_r($data); // Array // ( // [0] => Array // ( // [Name] => John // [age] => 27 // [ip] => 8.8.8.9 // ) // // [1] => Array // ( // [Name] => Sofia // [age] => 34 // [ip] => 8.8.8.10 // ) // // )
← how to use this in app?
create log ...inout