Q:

PHP program to decode the JSON string into a multi-dimensional array

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now