Skip to content
  • Homepage
  • HTML
  • CSS
  • Symfony
  • PHP
  • How to
  • Contact
  • Donate

Teach Developer

Articles, Guides & Tips

How to Convert PHP CSV to JSON

Home  »  PHP • Top Tutorials   »   How to Convert PHP CSV to JSON
Posted on June 10, 2023June 10, 2023
724

JSON format is a widely used format when dealing with API development. Most of the existing API responses are in JSON format.

Converting CSV content to JSON format in PHP is easy. In this article, we will look at various methods of achieving this conversion.

<?php 
$csvFileContent= file_get_contents("example.csv");
// Converts the CSV file content into line array 
$csvLineArray = explode("\n", $csvFileContent);
// Forms row results in an array format
$result = array_map("str_getcsv", $csvLineArray);
$jsonObject = json_encode($result);
print_r($jsonObject);
?>

The above quick example in PHP converts the CSV file content into JSON with few lines of code.

  1. First, it reads the .csv file content using the PHP file_get_contents() function.
  2. It explodes the CSV row by the new line (\n) escape sequence.
  3. Then, it iterates the line array and reads the line data of the CSV row.
  4. Finally, the resultant CSV row array is converted to JSON using the json_encode() function.

In step 3, the iteration happens with a single line of code. This line maps the array to call  PHP str_getcsv to parse and convert the CSV lines into an array.

The below input file is saved and used for this PHP example.

Input CSV

Id,Name,Type,Role
1,Lion,Wild,"Lazy Boss"
2,Tiger,Wild,CEO
3,Jaguar,Wild,Developer

Output JSON

This PHP quick example displays the below JSON output on the browser.

[["Id","Name","Type","Role"],["1","Lion","Wild","Lazy Boss"],["2","Tiger","Wild","CEO"],["3","Jaguar","Wild","Developer"]]

PHP, Top Tutorials

Post navigation

Previous Post: How to Check Twig Version in Symfony
Next Post: How to use events listeners and Event Subscriber in Symfony

Related Posts

  • PHP 8.1: read-only properties
  • Common Mistakes with Conditionals
  • Dealing with deprecations
  • How to remove index.php from URL in CodeIgniter 4
  • How to check the PHP version
  • How to use events listeners and Event Subscriber in Symfony

Categories

  • Codeigniter (3)
  • CSS (11)
  • eCommerce (1)
  • Framework (1)
  • Git (3)
  • How to (43)
  • HTML (5)
  • JavaScript (15)
  • Jquery (7)
  • Laravel (1)
  • Linux (4)
  • Magento-2 (1)
  • Node js (4)
  • Others (2)
  • PHP (11)
  • React (13)
  • Server (1)
  • SSH (3)
  • Symfony (6)
  • Tips (16)
  • Top Tutorials (10)
  • Ubuntu (3)
  • Vue (1)
  • Wordpress (7)

Latest Posts

  • What is SSH in Linux?
  • How to Delete Files in Ubuntu Command Line
  • How to Deploy a React application on a cPanel
  • How to use events listeners and Event Subscriber in Symfony
  • How to Convert PHP CSV to JSON

WEEKLY TAGS

AJAX (1) Codeigniter (1) Javascript (11) JQuery (1) PHP (16) Programming (1) React (3) Symfony (1)

Random Post

How to use :hover to modify the CSS of another class?
How to Deploy a React application on a cPanel
How to Search Recently Modified Files in Linux
How to use the Local Storage in Javascript
15 Best Free Open Source eCommerce Platforms

Quick Navigation

  • About
  • Contact
  • Privacy Policy

© Teach Developer 2021. All rights reserved