How Json file works with PHP ( Encode & Decode Json with PHP )

json_decode json_encode with php

PHP with Json Files ( Encode & Decode Json with PHP )

Here we are going to discuss about how to handle json file with PHP codes. Since API things came in market , handling json file will become a hot trend and every server side language is having functions to encode and decode them. PHP also have functions for handling json files , that is what we will discuss in this tutorial. So lets start without wasting our time …..

How to Decode json file ?

Lets assume we got a json file from an external server and we have to utilize the data inside it for our web page. ( eg : crypto trading sites are giving json data with their API for market price )

Here is a sample json data we are going to handle.

    "top1" : "200",
    "top2" : "198",
    "top3" : "189"


From the above data we want to utilize ” sitestats ” data on our webpage. So what code we will write in PHP to get those data from json file. Here it is

    $top3 = file_get_contents('data1.json');	
    $top3array = json_decode($top3,true);
    $top3array = $top3array['sitestats'];
    foreach($top3array as $top3)
        echo '<div>'.$top3['totalpaid'].'</div>';
        echo '<div>'.$top3['totalusers'].'</div>';
        echo '<div>'.$top3['totalcountry'].'</div>';

Lets discuss a little bit about above code.

  • If we see in json file and having idea of array , those json files are holding data in arrays actually and our sample json file is having 2 arrays with keywords ‘ top3daily ‘ and ‘ sitestats ‘.
  • $top3 is a php variable which will collect jason file details inside it.
  • $top3array is the php variable which will decode json file and hold its values which will come in array.
  • We want only values of  ‘sitestats ‘ that is the keyword for the array holding data. So $top3array[‘sitestats’] is an array now holding 3 values.
  • foreach loop will break that array and extract values of ‘ sitestats ‘ and echo them where every we want.


How to Encode json file ?

In this portion we are going to cover how to encode our own data into json file.  Our websites usually having some basic data which hardly change regularly for example its title , slogan , logo , address , social media links etc. Instead of saving them in database and un-necessary taking data from Database each time a user visit , why not store them into json file and use them directly ? is this a good idea or bad idea ? make a comment and let us know.

So we are going to encode our site setting data and make a json file for it.

$response = array();
$sitestats = array();
$sitestats[] = array(
'websitename' => 'Guideblog',
'websiteurl' => '',
'logo' => './images/logo.png',
'youtube' => ''	
$response['settings'] = $sitestats;
$fp = fopen('settings.json', 'w');
fwrite($fp, json_encode($response));

In the above php code we took 2 variables for making Arrays. One is $response and another is $sitestats.

$sitestats will collect array details of websitename , websiteurl , logo and youtube channel link.

Then this $sitestats will send its array details to $response with the keyword of settings and then fopen will create a json file with the name settings.json and fwrite will write all the json code inside it , then after file will get closed using fclose.


Output for the above code in json is



Are you interested in More PHP Tutorials ?


Leave a Reply

Your email address will not be published. Required fields are marked *

  +  24  =  29