PHP Tutorial

PHP Examples

PHP Practice

PHP program to demonstrate the json_encode() function with multi-dimensional array

Here, we are going to demonstrate the json_encode() function with multi-dimensional array in PHP.
Submitted by Nidhi, on November 23, 2020 [Last updated : March 13, 2023]

json_encode() with Multi-Dimensional Array

Here, we will create a multi-dimensional array that contains the employee records and then convert the array into JSON format using json_encode() function.

PHP code to demonstrate the example of json_encode() function with multi-dimensional array

The source code to demonstrate the json_encode() function with a multi-dimensional array is given below. The given program is compiled and executed successfully

<?php
//PHP program to demonstrate the json_encode() function 
//with multi-dimensional array.
$Emps = array(
    array(
        101,
        "Amit",
        5000
    ) ,
    array(
        102,
        "Rahul",
        7000
    ) ,
    array(
        103,
        "Rohit",
        8000
    )
);

$jsonStr = json_encode($Emps);
printf("Result: %s<br>", $jsonStr);

?>

Output

Result: [[101,"Amit",5000],[102,"Rahul",7000],[103,"Rohit",8000]]

Explanation

Here, we created a multi-dimensional array that contains the employee records. Here we used library function json_encode() that will convert the multi-dimensional array into a JSON string and assigned to the variable $jsonStr. After that we printed the $jsonStr on webpage using printf() function.

PHP JSON Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.