Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php array to csv

Instead of writing out values consider using 'fputcsv()'.

This may solve your problem immediately.

function array2csv($data, $delimiter = ',', $enclosure = '"', $escape_char = "")
{
    $f = fopen('php://memory', 'r+');
    foreach ($data as $item) {
        fputcsv($f, $item, $delimiter, $enclosure, $escape_char);
    }
    rewind($f);
    return stream_get_contents($f);
}

$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);
var_dump(array2csv($list));

/*
I hope it will help you.
Namaste
Stay Home Stay Safe
*/
Comment

csv to array in 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

php read csv file into array

$file = fopen('myCSVFile.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
  //$line is an array of the csv elements
  print_r($line);
}
fclose($file);
Comment

php read csv to array

$lines =file('CSV Address.csv');

foreach($lines as $data)
{
list($name[],$address[],$status[])
= explode(',',$data);
}
Comment

csv to array php

$csv = array_map('str_getcsv', file('data.csv'));
Comment

PREVIOUS NEXT
Code Example
Php :: how to get local current time in laravel 
Php :: php copy 
Php :: php controller 
Php :: get single column value in laravel eloquent 
Php :: How to validate a file type in laravel 
Php :: lat long in laravel validation 
Php :: To perform the requested action, WordPress needs to access your web server. Please enter your FTP 
Php :: routing code in drupal 8 
Php :: php money_format currency symbol 
Php :: php or 
Php :: Deleting an element from an array in PHP 
Php :: change php version in linux 
Php :: carbon get time 
Php :: percentage in php 
Php :: laravel insert timestamp now 
Php :: php eliminar elementos vacios array 
Php :: auth.php Class "AppUser" not found 
Php :: eloquent get only some columns 
Php :: 18 year back date in php 
Php :: file put contents append 
Php :: get country from clouflare 
Php :: send email template via php 
Php :: laravel collection transform 
Php :: laravel merge collections 
Php :: add two numbers as string in php 
Php :: show alert in php 
Php :: laravel run schedule locally 
Php :: laravel get image extension 
Php :: laravel curl package 
Php :: laravel 6 get user id 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =