How to convert JSON string to PHP Array?

JSON is a well-formatted structure of data which that used to exchange data on the web. JSON is the abbreviation of JavaScript Object Notation and it is a lightweight data-interchange format. It is easy to write and read data from JSON.

It supports data structures like object and array nicely. So there should be the way of converting JSON string into an array.

In this post, I am going to show you how to convert a JSON string easily into an array in PHP programming language.

Convert JSON string into PHP array

Converting a JSON formatted string into a PHP array is very easy. There is an in built PHP function available which can do this task nicely.

json_decode() is the PHP function that can be used to create an array from JSON format. It will decode a JSON formatted string and make it an array.

Example:

Suppose we have a JSON formatted data of OpenWeatherMap that you can see below:

{
  "coord": {
    "lon": -0.13,
    "lat": 51.51
  },
  "weather": [
    {
      "id": 300,
      "main": "Drizzle",
      "description": "light intensity drizzle",
      "icon": "09d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 280.32,
    "pressure": 1012,
    "humidity": 81,
    "temp_min": 279.15,
    "temp_max": 281.15
  },
  "visibility": 10000,
  "wind": {
    "speed": 4.1,
    "deg": 80
  },
  "clouds": {
    "all": 90
  },
  "dt": 1485789600,
  "sys": {
    "type": 1,
    "id": 5091,
    "message": 0.0103,
    "country": "0",
    "sunrise": 1485762037,
    "sunset": 1485794875
  },
  "id": 2643743,
  "name": "New York",
  "cod": 200
}

Now below is the syntax of json_decode PHP function that can decode JSON:

json_decode($dummyJson);

Where $dummyJson is the variable that stores the JSON string. Below is the complete code which you can run to see the result:

<?php

$dummyJson = '{
  "coord": {
    "lon": -0.13,
    "lat": 51.51
  },
  "weather": [
    {
      "id": 300,
      "main": "Drizzle",
      "description": "light intensity drizzle",
      "icon": "09d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 280.32,
    "pressure": 1012,
    "humidity": 81,
    "temp_min": 279.15,
    "temp_max": 281.15
  },
  "visibility": 10000,
  "wind": {
    "speed": 4.1,
    "deg": 80
  },
  "clouds": {
    "all": 90
  },
  "dt": 1485789600,
  "sys": {
    "type": 1,
    "id": 5091,
    "message": 0.0103,
    "country": "0",
    "sunrise": 1485762037,
    "sunset": 1485794875
  },
  "id": 2643743,
  "name": "New York",
  "cod": 200
}';

$json_to_arr = json_decode($dummyJson);

print_r($json_to_arr);

?>

Now if you run the above code, you will see an array that looks like below:

stdClass Object ( [coord] => stdClass Object ( [lon] => -0.13 [lat] => 51.51 ) [weather] => Array ( [0] => stdClass Object ( [id] => 300 [main] => Drizzle [description] => light intensity drizzle [icon] => 09d ) ) [base] => stations [main] => stdClass Object ( [temp] => 280.32 [pressure] => 1012 [humidity] => 81 [temp_min] => 279.15 [temp_max] => 281.15 ) [visibility] => 10000 [wind] => stdClass Object ( [speed] => 4.1 [deg] => 80 ) [clouds] => stdClass Object ( [all] => 90 ) [dt] => 1485789600 [sys] => stdClass Object ( [type] => 1 [id] => 5091 [message] => 0.0103 [country] => 0 [sunrise] => 1485762037 [sunset] => 1485794875 ) [id] => 2643743 [name] => New York [cod] => 200 )

Obviously, the above array is not easy to read for the human. To make it easy for human reading we need to add the pre HTML tag just like below:

echo "<pre>";
print_r($json_to_arr);
echo "</pre>";

Now you will see the output looks like below:

stdClass Object
(
    [coord] => stdClass Object
        (
            [lon] => -0.13
            [lat] => 51.51
        )

    [weather] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 300
                    [main] => Drizzle
                    [description] => light intensity drizzle
                    [icon] => 09d
                )

        )

    [base] => stations
    [main] => stdClass Object
        (
            [temp] => 280.32
            [pressure] => 1012
            [humidity] => 81
            [temp_min] => 279.15
            [temp_max] => 281.15
        )

    [visibility] => 10000
    [wind] => stdClass Object
        (
            [speed] => 4.1
            [deg] => 80
        )

    [clouds] => stdClass Object
        (
            [all] => 90
        )

    [dt] => 1485789600
    [sys] => stdClass Object
        (
            [type] => 1
            [id] => 5091
            [message] => 0.0103
            [country] => 0
            [sunrise] => 1485762037
            [sunset] => 1485794875
        )

    [id] => 2643743
    [name] => New York
    [cod] => 200
)

The above array is easy to understand for a human. But in real life or in the project you don’t have to show the array to users. But it can be used at the time of developing. Many developers and even I also use <pre> tag several times in the time of working on a project in our company.

I hope, you have understood how to convert a JSON formatted string to an array.

Leave a Reply

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