Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP reading csv column into array

Tags:

php

I there a php function that enables me to read a csv column (COLUMN NOT LINE) into an array or a string ?

thank you in advance.

like image 920
Joseph Avatar asked Jul 15 '13 12:07

Joseph


People also ask

How to get CSV data in array in PHP?

Using array_map() to Read a CSV File Alternatively, you can use the array_map() function to read a CSV file into an array. To do this, you'll use str_getcsv as a callback function. This is a built-in PHP function that is used to parse a CSV string into an array.

How to use fgetcsv in PHP?

PHP fgetcsv() Function$file = fopen("contacts. csv","r"); print_r(fgetcsv($file)); fclose($file);


2 Answers

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

$col = array_search("Value", $header, true); 
 foreach ($csv as $row) {      
 $array[] = $row[$col]; 
}
like image 198
Goran Avatar answered Oct 20 '22 07:10

Goran


May this will help. http://www.php.net/manual/de/function.fgetcsv.php Just grab the position of the row for the column you want to have.

like image 37
user2140111 Avatar answered Oct 20 '22 07:10

user2140111