JSON stands for JavaScript Object Notation and is the standard format to store and transfer data on the web. The format is easy to work with in PHP thanks to its built-in functions to parse, encode, and decode it.

JSON is a string-representation of data that supports the following values:

  • string
  • number
  • object
  • array
  • boolean
  • null

Here's some sample JSON containing data about a country:

JSON
{ name: "Wakanda", popualation: "6000000", continent: "Africa", tribes: ["Panther Tribe", "Border Tribe", "Mining Tribe", "River Tribe", "Merchant Tribe"], leader: { name: "T'Challa", gender: "Male" } }

In general, as you can see above, data is stored in JSON format using key/value pairs.

Encoding JSON in PHP

You can encode data to JSON using PHP's built-in json_encode() function. You can encode most things, including arrays and any primitive types.

Here's an example of encoding an array:

PHP
<?php $data = ["Hello" => 1, "World" => 2]; $json = json_encode($data); echo($json); ?>
JSON
{"Hello":1,"World":2}

Here's an example of encoding an object:

PHP
<?php $person->name = "Blake"; $person->age = 26; $person->city = "New York City"; $json = json_encode($person); echo($json); ?>
BASH
{"name":"Blake","age":26,"city":"New York City"}

In both cases, $json contains a string of valid JSON that you can then do anything you want with it, including sending it to the client or writing it to a file.

Decoding JSON in PHP

Just like you can encode JSON, you can also decode JSON using PHP's built-in json_decode() function. Pass in a string of JSON and PHP will give you back a PHP object:

PHP
<?php $json = '{"name":"Blake","age":26,"city":"New York City"}'; $object = json_decode($json); var_dump($object); ?>
HTML
object(stdClass)#1 (3) { ["name"]=> string(5) "Blake" ["age"]=> int(26) ["city"]=> string(13) "New York City" }

From here you can then access a single value or iterate through all the values, like with any object:

PHP
<?php $json = '{"name":"Blake","age":26,"city":"New York City"}'; $object = json_decode($json); echo($object->name . "<br>"); foreach ($object as $key => $value) { echo($key . "=>" . $value . "<br>"); } ?>
BASH
Blake name=>Blake age=>26 city=>New York City

Alternatively, if you want an associative array as the return value instead of an object, you can specify that by passing in true as a second parameter. This tells PHP to give you the return value as an associative array. The default value is false.

PHP
<?php $json = '{"name":"Blake","age":26,"city":"New York City"}'; $array = json_decode($json, true); var_dump($array); ?>
HTML
array(3) { ["name"]=> string(5) "Blake" ["age"]=> int(26) ["city"]=> string(13) "New York City" }

Like before, you can access data from the array like any associative array:

PHP
<?php $json = '{"name":"Blake","age":26,"city":"New York City"}'; $array = json_decode($json, true); echo($array["city"] . "<br>"); foreach ($array as $key => $value) { echo($key . "=>" . $value . "<br>"); } ?>
BASH
New York City name=>Blake age=>26 city=>New York City
Next Lesson »
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.