Skip to Content
PHPCODE
Converting an Array to JSON in PHP Live demo step by step
jsonphp code / November 9, 2020

JSON to array online in PHP converter code

So guys, let’s learn JSON to array online in PHP, In our previous tutorial, we have explained how to convert the associative array into XML in PHP. In this tutorial, we will explain how to convert an array to a JSON object in PHP.

JSON stands for JavaScript Object Notation. Currently, JSON is the most used data format to handle data. The major use of JSON is to fetch and return data in JSON format from the server and to use the JSON data at the client end.

While developing APIs, data is accessed from the database and converted to JSON data to return response in JSON format. In PHP, we can easily convert an array to JSON with json_encode() function. For example, if we get data from a database or have data in an associative array with the key value. Then we can easily convert this array to a JSON object using PHP.

Here is the data in an associative array with key-value:

<?php
$empData = array (       
    array( 
        "name" => "Jhon Smith", 
        "age" => "40"
    ), 
    array( 
        "name" => "Kane William", 
        "age" => "50"
    ), 
    array( 
        "name" => "Ian David", 
        "age" => "30"
    ) 
); 
echo json_encode($empData); 

?>

JSON to array online in PHP converter code

We will run above code, it will convert the $empData array into JSON data using json_encode() function. Below is the JSON output from above code example. [{“name”:”Jhon Smith”,”age”:”40″}, {“name”:”Kane William”,”age”:”50″}, {“name”:”Ian David”,”age”:”30″}]
PHPCODE © 2024