belongs to collection: PHP JSON Programs
Here, we will convert a JSON string into a multi-dimensional array using the json_decode() function and print the elements of the multi-dimensional array on the webpage.
Program/Source Code:
The source code to decode the JSON string into a multi-dimensional array is given below. The given program is compiled and executed successfully.
<?php //PHP program to decode the Json string into //multi-dimensional array. $json = '[[101,"Amit",5000],[102,"Rahul",7000],[103,"Rohit",8000]]'; $emps = json_decode($json); for ($i = 0;$i < 3;$i++) { for ($j = 0;$j < 3;$j++) { print ($emps[$i][$j] . " "); } echo "<br/>"; } ?>
Output:
101 Amit 5000 102 Rahul 7000 103 Rohit 8000
Explanation:
Here, we converted the JSON string into a multi-dimensional array using library function json_decode() and assigned the result into $emps variable.
for ($i = 0; $i < 3; $i++) { for ($j = 0; $j < 3; $j++) { print($emps[$i][$j]." "); } echo ""; }
Here, we printed the employee records contained in a multi-dimensional array using a foreach loop on the webpage.
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Program/Source Code:
The source code to decode the JSON string into a multi-dimensional array is given below. The given program is compiled and executed successfully.
Output:
Explanation:
Here, we converted the JSON string into a multi-dimensional array using library function json_decode() and assigned the result into $emps variable.
Here, we printed the employee records contained in a multi-dimensional array using a foreach loop on the webpage.
need an explanation for this answer? contact us directly to get an explanation for this answer