Skip to content
  • Home
  • Blog
    • yttags
    • Youtube
  • Categories
  • Tools
  • Server-Side
    • php
    • Node.js
    • ASP.NET
    • Magento
    • Codeigniter
    • Laravel
    • Yii
  • JS
    • AngularJS
    • Ajax
    • VueJs
    • jQuery
    • ReactJS
    • JavaScript
  • Full Form
  • Guest Post
  • Advertise
  • About
  • Contact Us
Pakainfo

Pakainfo

Web Development & Good Online education

php read csv file into array Examples

February 1, 2021 Pakainfo php, Mysql, Mysqli Leave a comment
Rate this post

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

<?php

$filename = 'cars.csv';

// The nested array to hold all the arrays
$pakainfo_articles = []; 

// Open the file for reading
if (($h = fopen("{$filename}", "r")) !== FALSE) 
{

  while (($data = fgetcsv($h, 1000, ",")) !== FALSE) 
  {
    $pakainfo_articles[] = $data;		
  }

  fclose($h);
}

// Display the code in a readable format
echo "<pre>";
var_dump($pakainfo_articles);
echo "</pre>";

import csv to array php

PHP Code (read csv php)

<?php
$row = 1;
if (($handle = fopen("pakainfo_articles.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\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 (<br>";
foreach($csv as $location => $data)
{
  echo " array (<br>";
  echo '  "member_code" => "' . $data[1] . '",<br>';
  echo '  "name" => "' . $data[2] . '",<br>';
  echo '  "nicname" => "' . $data[3] . '",<br>';
  echo '  "lat" => "' . $data[4] . '",<br>';
  echo '  "lng" => "' . $data[5] . '"<br>';
  echo " ),<br>";
}
echo ");";

That produced an array that looked like this:

Free Live Chat for Any Issue

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"
 ),
);

Download

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.

Read Also:  Convert number to words in PHP

Related posts:

  1. PHP wordwrap Text file to array String to Date Functions
  2. Array to string conversion laravel 6/7/8 Best Examples
  3. File handling READ AND WRITE TEXT FILE IN ASP.NET C#, VB
  4. PHP Array Tutorial With Examples – filter array
  5. How to Read Text File in Javascript Line By Line?
  6. Read Connection String from Web.Config file in ASP.Net using C# and VB.Net
  7. Bash For Loop Examples (For and Read-While Loops in Bash)
  8. Converting CSV file into an Associative Array in PHP
  9. php array to string – The Best PHP 3 Examples
  10. Convert Array to XML and XML to Array using PHP
Read Also:  PHP image manipulation Fits and Fill
csv to php array onlinehow to read csv file column wise in phplaravel csv to arrayphp csv string to arrayphp get first row of csv filephp loop through csvphp read csv file into associative arrayphp read csv file line by line

Post navigation

Previous Post:PHP chat code in php with demo – chat in PHP with example script
Next Post:php foreach multidimensional array Example

Search

Write For Us

We’re accepting well-written informative guest posts and this is a great opportunity to collaborate.
Submit a guest post to [email protected]
Contact Us

Freelance web developer

Do you want to build a modern, lightweight, responsive website quickly?
Need a Website Or Web Application Contact : [email protected]
Note: Paid Service
Contact Me
Cricday

Categories

3movierulz (48) Ajax (464) AngularJS (377) ASP.NET (61) Bollywood (92) Codeigniter (174) CSS (96) Earn Money (61) Education (53) Entertainment (108) fullform (77) Google Adsense (62) Highcharts (77) Hollywood (93) JavaScript (1354) Jobs (39) jQuery (1421) Laravel (1083) LifeStyle (50) movierulz4 (47) Mysql (1029) Mysqli (890) Node.js (38) php (2110) Programming (2320) Python (96) ReactJS (37) Software (101) Software (77) Stories (78) tamilrockers (88) Tamilrockers kannada (48) Tamilrockers telugu (47) Tech (101) Technology (2359) Tips and Tricks (107) Tools (110) Top10 (291) Trading (49) Trending (45) VueJs (250) Web Technology (83) webtools (128) wordpress (165) World (118)

Advertise With Us

Increase visibility and sales with advertising. Let us promote you online.
Click Here

A To Z Full Forms

Access a complete full forms list with the meaning, definition, and example of the acronym or abbreviation.
Click Here

Web Development & Good Online education : Pakainfo by Pakainfo.
Top
Subscribe On YouTube : Download Source Code & New itsolutionstuck
We accept paid guest Posting on our Site : Guest Post Chat with Us On WhatsApp