Q:

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

belongs to collection: PHP JSON Programs

0

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.

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 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.

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

total answers (1)

PHP program to decode the JSON string into an asso... >>
<< PHP program to demonstrate the json_encode() funct...