Today, We want to share with you php read csv file into array.In this post we will show you php loop through csv, hear for How to create an array from a CSV file using PHP and the fgetcsv function? we will give you demo and example for implement.In this post, we will learn about php read csv file into associative array with an example.
How To Read CSV Files With PHP And Convert To Array?
$file = fopen('pakainfo_articles.csv', 'r'); while (($line = fgetcsv($file)) !== FALSE) { //$line is an array of the csv elements print_r($line); } fclose($file);
Converting The CSV To An Array
if (($handle = fopen('pakainfo_articles.csv', 'r')) !== FALSE) { // Check the resource is valid while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { // Check opening the file is OK! print_r($data); // Array } fclose($handle); }
How to convert a CSV file into a PHP multidimensional array?
index.php
"; var_dump($pakainfo_articles); echo "
“;
import csv to array php
PHP Code (read csv php)
$num fields in line $row:
\n"; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . "
\n"; } } fclose($handle); } ?>
How To Convert A CSV File Into An Array In PHP?
index.php
$csv = array_map('str_getcsv', file('pakainfo_articles.csv')); echo "array (
"; foreach($csv as $location => $data) { echo " array (
"; echo ' "member_code" => "' . $data[1] . '",
'; echo ' "name" => "' . $data[2] . '",
'; echo ' "nicname" => "' . $data[3] . '",
'; echo ' "lat" => "' . $data[4] . '",
'; echo ' "lng" => "' . $data[5] . '"
'; echo " ),
"; } echo ");";
That produced an array that looked like this:
array ( array ( "member_code" => "3565", "name" => "bhavika Pethani", "nicname" => "KDJ", "lat" => "-32.86328", "lng" => "140.80375" ), array ( "member_code" => "9898", "name" => "Abba River", "nicname" => "WA", "lat" => "-33.68488", "lng" => "118.46334" ), array ( "member_code" => "4586", "name" => "4cgandhi", "nicname" => "MS", "lat" => "-33.66077", "lng" => "250.25863" ), );
I hope you get an idea about PHP fgetcsv() Function.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.