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

column of csv to array php

$csv = array_map("str_getcsv", file("data.csv", "r")); 
$header = array_shift($csv); 
// Seperate the header from data

$col = array_search("Value", $header); 
 foreach ($csv as $row) {      
 $array[] = $row[$col]; 
}
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 :: laravel migration add date of birth column 
Php :: php convert hex to rgba 
Php :: laravel websockets onclose 
Php :: Too Many Attempts. laravel error 
Php :: remove decimal php 
Php :: php odd or even 
Php :: laravel get url without domain in blade 
Php :: wordpress acf get field 
Php :: laravel table in model 
Php :: composer larave install version 
Php :: laravel encrypt password 
Php :: count() parameter must be an array or an object that implements countable laravel 
Php :: laravel get file contents from storage 
Php :: php usort keep keys 
Php :: wordpress get current logged in user 
Php :: php current page url 
Php :: format datetime ISO php 
Php :: php check for empty string 
Php :: php object(stdclass) to array 
Php :: php set alternatives 
Php :: How to install a specific version of package using Composer? 
Php :: Warning: mysqli_fetch_all() expects parameter 1 to be mysqli_result, bool given in C: ewxammphtdocslearnindex.php on line 11 
Php :: randstring php 
Php :: php write to file 
Php :: php utf8_decode 
Php :: string to int php 
Php :: codeigniter order by random 
Php :: php multidimensional array get all values by key 
Php :: php http_build_query 
Php :: generate random string php 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =