dph87312 2018-06-15 06:06 采纳率: 100%
浏览 137
已采纳

使用分隔符将CSV数据转换为array_map和str_getcsv

My problem is I'm unable to pass 2nd parameter to str_getcsv function.

$rows   = array_map('str_getcsv', file($filePath_product_names_t), [";"]); 
            $header = array_shift($rows);
            $csv    = [];
            foreach($rows as $row) {
                $csv[] = array_combine($header, $row);
            }

CSV structure

"25";"some text"; "also some Text"

And also getting WARNING:

array_combine(): Both parameters should have an equal number of elements

  • 写回答

2条回答 默认 最新

  • dongzantai7570 2018-06-15 06:28
    关注

    Try this:

    $rows = array_map(function($v){return str_getcsv($v, ";");}, file($filePath_product_names_t));
    $header = array_shift($rows);
    $csv    = [];
    foreach($rows as $row) {
        $csv[] = array_combine($header, $row);
    }
    print_r($rows);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?