ADVERTISEMENT
ADVERTISEMENT

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

Here, we are going to learn how to decode the JSON string into a multi-dimensional array in PHP?
Submitted by Nidhi, on November 23, 2020

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.

PHP JSON Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.