How to extract data from csv file in PHP

前端 未结 11 1539
醉酒成梦
醉酒成梦 2020-11-22 07:36

I have a csv file which looks like this

$lines[0] = \"text, with commas\", \"another text\", 123, \"text\",5;
$lines[1] = \"some without commas\", \"another          


        
11条回答
  •  旧巷少年郎
    2020-11-22 08:15

    Return a php mapping array with the column of interests :

    public function extractCSVDatas($file_uri) {
        $AliasToSystemPathMappingArray = [];
        if (($handle = fopen($file_uri, "r")) !== FALSE) {
          $csv = array_map('str_getcsv', file($file_uri));
    
          //remove header and choose columns among the list:
          foreach((array_slice($csv,1)) as $line) {
            list($id, $alias, $systemPath) = explode(';',$line[0]);
            $AliasToSystemPathMappingArray[] = [$alias, $systemPath];
          }
          fclose($handle);
        }
        return $AliasToSystemPathMappingArray;
      }
    

提交回复
热议问题