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.
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);
?>
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.
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
Output:
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