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

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 Error Log, How to check Error Log in Laravel 
Php :: get values from text file php 
Php :: laravel multiple where conditions 
Php :: foreach stdclass object php 
Php :: load database in codeigniter 
Php :: php pdo update 
Php :: percentage in php 
Php :: laravel wher in 
Php :: php list all constants 
Php :: laravel include with variable 
Php :: php delete json object from a collection object 
Php :: use auth automatic login on register 
Php :: get array key based on value php 
Php :: header refresh page php 
Php :: php text to html 
Php :: IlluminateDatabaseEloquentCollection to array 
Php :: php cloudflare get country from IP 
Php :: validate user password laravel8 
Php :: file storage laravel 
Php :: take file data in variable php 
Php :: part of url php 
Php :: how make factory and seeder in laravel 8 
Php :: how to remove null values in array php 
Php :: how to print in php 
Php :: types of controller in laravel 
Php :: create function php 
Php :: php override trait method and call it 
Php :: php replace string within string 
Php :: convert single quote in htmlentities php 
Php :: return view in laravel controller 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =