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
569

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

  • TOP 10 MOST POPULAR PROGRAMMING LANGUAGES IN 2022
  • How to check the PHP version
  • How to split a string into an array with Twig
  • How to Deploy a React application on a cPanel
  • PHP 8: Attributes
  • How to PHP Get Max Value From Array

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 check the PHP version
How to Deploy or Host your ReactJS App in cPanel
10 Programming Habits that every Developer Should Adopt
Difference Between Git and GitHub
Dealing with deprecations

Quick Navigation

  • About
  • Contact
  • Privacy Policy

© Teach Developer 2021. All rights reserved