PHP CSV string to array

前端 未结 10 1287
攒了一身酷
攒了一身酷 2020-11-27 02:41

I\'m trying to parse a CSV string to an array in PHP. The CSV string has the following attributes:

Delimiter: ,
Encl         


        
10条回答
  •  失恋的感觉
    2020-11-27 03:35

    Do this:

    $csvData = file_get_contents($fileName);
    $lines = explode(PHP_EOL, $csvData);
    $array = array();
    foreach ($lines as $line) {
        $array[] = str_getcsv($line);
    }
    print_r($array);
    

    It will give you an output like this:

    Array
    (
        [0] => Array
            (
                [0] => 12345
                [1] => Computers
                [2] => Acer
                [3] => 4
                [4] => Varta
                [5] => 5.93
                [6] => 1
                [7] => 0.04
                [8] => 27-05-2013
            )
    
        [1] => Array
            (
                [0] => 12346
                [1] => Computers
                [2] => Acer
                [3] => 5
                [4] => Decra
                [5] => 5.94
                [6] => 1
                [7] => 0.04
                [8] => 27-05-2013
            )
    
    )
    

    I hope this can be of some help.

提交回复
热议问题