Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

csv file to associative array php

$array = $fields = array();
        $handle = @fopen("yourcsvfilename.csv", "r");
        if($handle){
            while(($row = fgetcsv($handle, 4096)) !== False){
                if(empty($fields)){
                    $fields = $row;
                    continue;
                }
                foreach($row as $k=>$value){
                    $array[$i][$fields[$k]] = $value;
                }
                $i++;
            }
            if(!feof($handle)){
                echo "Error: unexpected fgets() fail
";
            }
            fclose($handle);
        }
        print_r($array);
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #csv #file #associative #array #php
ADD COMMENT
Topic
Name
8+2 =