How To Get Data From Json Array In PHP
Last Updated : Mar 11, 2024
IN - PHP | Written & Updated By - Dikshita
In this tutorial we will show you the solution of how to get data from json array in PHP and all can be done using json_decode() function. Let us understand this article with help of json decode.
As, there are many other ways with help of which we can get data from json array but in this article we are going to use the basic way or say most preferred way which is used most times.
Step By Step Guide On How To Get Data From Json Array In PHP :-
Let us understand this json decode with help of codes. One thing to show here the basic syntax of json_decode() as json_decode($jsonString, $assoc, $depth, $options);
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title> how to get data from json array in php </title> </head> <body> <?php $jsonData ='{ "firstName":"Tony", "lastName":"Stark" }'; $output = json_decode($jsonString); var_dump($output); ?> </body> </html>
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
- Here, inside body here we create basic structure of php. In which we use <?php and ?>.
- Inside these tags, we create a variable name jsonData as you can see above and inside this we assigns some values to its keys like first name and last name. You can use here as much values you want.
- After that we just use json_decode() and pass a parameter on which we want to apply json_decode() function. After this we store the resulted values inside a variable named output.
- Now, our last job is to print the required output or show the data. So, that we use var_dump which is a predefind function of php. it is used to dump information and it also tells us about the datatype used. It is mostly used to give structured value of php.
- Here inside var_dump() we directly pass our variable that is output. In which we store our result. We hope that you understand codes properly.
- At last, the <body> and <html> tags are closed with </body> and </html> respectively.
Conclusion :-
At last in conclusion, here we can say that with the help of this article we are able to understand how to get data from json array in php.
I hope this tutorial on how to get data from json array in PHP helps you and the steps and method mentioned above are easy to follow and implement