1. 程式人生 > >PHP csv文件內容轉成數組/Json

PHP csv文件內容轉成數組/Json

true enc 內容 [] etc json php pretty count

$lines = array_map(‘str_getcsv‘, file($filePath));; 

$result = array();
$headers = null;

if (count($lines) > 0) {
    $headers = $lines[0];
}

for($i=1; $i<count($lines); $i++) {
    $obj = $lines[$i];
    $result[] = array_combine($headers, $obj);//轉成數組
}

$json =  json_encode($result, JSON_PRETTY_PRINT);//轉成JSON

  

PHP csv文件內容轉成數組/Json