Answers for "php efficient csv to array"

PHP
1

php csv to array with header

function parse($file, $separ = ';')
{
    $arrays = array_map(function ($foo) use ($separ) {
        return array_map("trim", str_getcsv($foo, $separ));
    }, file($file, FILE_SKIP_EMPTY_LINES));

    $header = $arrays[0];
    unset($arrays[0]);

    $array_with_keys = [];
    foreach ($arrays as $array) {
        $_array = [];
        foreach ($array as $key => $value) {
            $_array[$header[$key]] = $value;
        }
        $array_with_keys[] = $_array;
    }

    return $array_with_keys;
}
Posted by: Guest on May-19-2021
1

php read csv to array

$lines =file('CSV Address.csv');

foreach($lines as $data)
{
list($name[],$address[],$status[])
= explode(',',$data);
}
Posted by: Guest on October-30-2020
0

csv to array php

$csv = array_map('str_getcsv', file('data.csv'));
Posted by: Guest on April-05-2021

Browse Popular Code Answers by Language