Search
 
SCRIPT & CODE EXAMPLE
 

PHP

How to create an array from a CSV file using PHP

public function csvToArray($filename = '', $delimiter = ','){
  if(!file_exists($filename) || !is_readable($filename))
  	return false;
  $header = null;
  $data = array();
  if(($handle = fopen($filename, 'r')) !== false){
  	while(($row = fgetcsv($handle, 1000, $delimiter)) !== false){
  		if(!$header)
  			$header = $row;
  		else
  			$data[] = array_combine($header, $row);
  	}
  	fclose($handle);
  }
  return $data;
}
    
    
Comment

csv file to associative array php

<?php
    /* Map Rows and Loop Through Them */
    $rows   = array_map('str_getcsv', file('file.csv'));
    $header = array_shift($rows);
    $csv    = array();
    foreach($rows as $row) {
        $csv[] = array_combine($header, $row);
    }
?>
Comment

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);
Comment

PREVIOUS NEXT
Code Example
Php :: display all errors in blade laravel 
Php :: php fpm config file location 
Php :: laravel subtract date 
Php :: php pdo check if update query successful 
Php :: install php 5.6 on ubuntu 18.04 
Php :: exec command not working in php but works in terminal 
Php :: php sort multi dimensional array 
Php :: how to search by sku woocommerce 
Php :: how to use flash message in laravel 
Php :: getMessage in php 
Php :: Composer Fatal error: Call to undefined function SymfonyPolyfillMbstringiconv() in phar 
Php :: laravel update table column 
Php :: php move file to another directory 
Php :: add days to date in php 
Php :: json to array php 
Php :: carbon subtract two dates 
Php :: upload file in php 
Php :: how to go to another folder in php 
Php :: symfony call service in controller 
Php :: Carbon fomart date 
Php :: unset by key name php 
Php :: store multiple session in laravel 
Php :: paginate relationship laravel7 
Php :: laravel check if exists in table 
Php :: comparing floats php 
Php :: how to read data from serial port in php 
Php :: Type cast using double php 
Php :: php count matching words in two strings 
Php :: php find first occurrence in string 
Php :: move post to draft php wordpress 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =