How to Use PHP to Read and Write from JSON Files

2 years ago admin PHP

In this lesson, we will see how to use PHP to read and write from JSON files first, we will see how to write an array of data inside the JSON file, and next, we will see how to fetch the written data from the JSON file.


Write data inside the JSON file

First, let's create a new file 'data.json' in which we will write an array of data.

                                                    
                                                                                                                
<?php

// Data to be written
$data = [
    'name' => 'john',
    'age' => '30'
];

// Convert data to JSON string
$jsonString = json_encode($data);

// Write data into the file
file_put_contents('data.json', $jsonString);

Fetch the written data from JSON file

Next, let's fetch the written data from the JSON file.

                                                        
                                                                                                                        
<?php

// Read data from file
$jsonString = file_get_contents('data.json');

// Convert data into array
$data = json_decode($jsonString, true);

// Display data
echo $data['name'].' '.$data['age'];

Related Tuorials

How to Use Variables and Data Types in PHP (A Clear Guide for Absolute Beginners) Part 2

In the second part of this guide, we will see more PHP types.Also, we will see how to check a variab...


How to Use Variables and Data Types in PHP (A Clear Guide for Absolute Beginners) Part 1

The first thing you need to understand when learning PHP is variables and data types. In this g...


How to Install and Run PHP on Your Localhost (Step-by-Step Guide)

If you want to learn PHP or build a web application with it, the first step is to create a working l...


CRUD Application with PHP PDO Ajax, and MySQL Part 2

In the second part of this tutorial, we will get all the students from the database and display them...


CRUD Application with PHP PDO Ajax, and MySQL Part 1

In this tutorial we will see how to create a crud application with PHP PDO Ajax, and MySQL, the user...


How to Sort Associative Arrays in Descending Order According to the Key Value in PHP

in this lesson, we will see how to sort associative arrays in descending order according to the key...


How to Sort Associative Arrays in Ascending Order According to the Key Value in PHP

in this lesson, we will see how to sort associative arrays in ascending order according to the key v...


How to Sort Associative Arrays in Descending Order According to the Value in PHP

in this lesson, we will see how to sort associative arrays in descending order according to the valu...


How to Sort Associative Arrays in Ascending Order According to the Value in PHP

in this lesson, we will see how to sort associative arrays in ascending order according to the value...


How Do you Sort an Array in Descending Order in PHP

In this lesson, we will see how to sort descending an array in PHP, we will use the rsort() function...