In this article Convert JSON to array PHP tutorial, we started with the What is JSON?
How JSON works with PHP. I divided this issue into 2 sections,
- Convert JSON string to Array
- Convert JSON string to object.
JSON and PHP array both are also used for data storing. Here we start with the introduction of JSON.
Introduction to JSON
- JSON stands for JavaScript Object Notation.
- JSON is used for data exchanging between browser and server.
- JSON basically is text and written in JS object Notation
When we sending or exchanging the data between server and browser the data must be in text format.
And JSON is text and we can be converting it into JS array, PHP array, and PHP object. In vise versa, we convert PHP array to JSON format and send it into the server.
JSON Syntax
1 2 3 4 5 |
{ "first_name" : "Bikash", "last_name" : "Panda", "location" : "LKO" } |
Introduction to PHP Array
I know you all know about PHP array. The array is also a special type of variable that can store one or more values at a time.
In PHP there are 3 types of array ,
- Indexed arrays – Arrays with a numeric index
- Associative arrays – Arrays with named keys
- Multidimensional arrays – Arrays containing one or more arrays
PHP Array Syntax
1 |
$names = array("John", "DOE", "Hakuna"); |
Convert JSON to Array PHP [ JSON decode ]
Now we check how we Convert JSON string to Array with example. From the above words, you all know about what is an array and JSON and how we declare JSON and array in PHP.
For converting JSON to array in PHP we use JSON decode json_decode()
function. So we check with a working example below,
Example JSON to Array PHP
1 2 3 4 5 6 7 8 9 10 |
<?php // JSON string $someJSON = '[{"first_name":"Bikash","gender":"male"},{"first_name":"Dev","gender":"male"},{"first_name":"John Doe","gender":"female"}]'; // Convert JSON string to Array $someArray = json_decode($someJSON, true); echo "<pre>"; print_r($someArray); // Dump all data of the Array echo $someArray[0]["first_name"]; // Access Array data ?> |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Array ( [0] => Array ( [first_name] => Bikash [gender] => male ) [1] => Array ( [first_name] => Dev [gender] => male ) [2] => Array ( [first_name] => John Doe [gender] => female ) ) Bikash |
Example JSON to PHP Object
1 2 3 4 5 6 7 8 9 10 |
<?php // JSON string $someJSON = '[{"first_name":"Bikash","gender":"male"},{"first_name":"Dev","gender":"male"},{"first_name":"John Doe","gender":"female"}]'; // Convert JSON string to Object $someObject = json_decode($someJSON); echo "<pre>"; print_r($someObject); // Dump all data of the Object echo $someObject[0]->first_name; // Access Object data ?> |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Array ( [0] => stdClass Object ( [first_name] => Bikash [gender] => male ) [1] => stdClass Object ( [first_name] => Dev [gender] => male ) [2] => stdClass Object ( [first_name] => John Doe [gender] => female ) ) Bikash |
You can copy that code and paste it with PHP tag here for live view,
If you want to convert array to JSON in PHP, you have to use json_encoded()
function.
Also check:
- Send Mail Attachments with PHP Mail()
- Selection in CSS | CSS ::selection
- How to Integrate Google Translate on website
- Javascript for loop complete reference
Happy Coding..!
[…] How to Convert JSON to Array PHP […]
[…] How to Convert JSON to Array PHP […]
[…] How to Convert JSON to Array PHP […]
[…] How to Convert JSON to Array PHP […]
[…] Convert JSON to Array PHP […]
[…] How to Convert JSON to Array PHP […]
[…] How to Convert JSON to Array PHP […]
[…] Also Read: How to Convert JSON to Array PHP […]