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 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 send data from PHP to Python 
Php :: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in 
Php :: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) in phar:///usr/local/Cellar/composer/1.9.1/bin/composer/src/Composer/DependencyResolver/Solver.php on line 223 mac 
Php :: laravel migration add date of birth column 
Php :: laravel migration remove constraint 
Php :: how to reverse fetch assoc in php 
Php :: Add 7 days to the current date in PHP 
Php :: laravel check pagination in blade 
Php :: php sort array by key 
Php :: laravel table in model 
Php :: eloquent where in 
Php :: php artisan route:list for specific name 
Php :: remove first character from string laravel 
Php :: confirm password validation in laravel 
Php :: php prime numbers 
Php :: factorial program in php using recursive function 
Php :: php key value dictionary 
Php :: laravel check if field has changed 
Php :: laravel migration with primary key 
Php :: php body_class wp 
Php :: php check of object is empty 
Php :: php date is before 
Php :: read file data using php 
Php :: nl2br php 
Php :: Laravel 9 Clear Cache of Route, View, Config, Event Commands 
Php :: php dump 
Php :: php create zip from folder 
Php :: save post data to file php 
Php :: random integer php 
Php :: check type in php 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =