A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Write a php program to loop over the json data
Q:

Write a php program to loop over the json data

0

PHP Json Encode

Write a php program to loop over the json data

Suppose, you have the following json data.


[
	{
	"name" : "John Garg",
	"age"  : "15",
	"school" : "Ahlcon Public school"
	},
	{
	"name" : "Smith Soy",
	"age"  : "16",
	"school" : "St. Marie school"
	},
	{
	"name" : "Charle Rena",
	"age"  : "16",
	"school" : "St. Columba school"
	}
]

All Answers

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

Solution

PHP allows us to easily convert JSON to arrays. PHP provides predefined functions to easily encode and decode json string into php array. The following program converts the json string into array and loop over them.

<!Doctype>
<html>
 <head>
	<title>PHP Json Encode</title>
 </head>
 <body>
	<table>
	<tr>
		<td><b>Name</b></td>
		<td><b>Age</b></td>
		<td><b>School</b></td>
	</tr>
	<?php
	$student_json = '[
			{
				"name" : "John Garg",
				"age"  : "15",
				"school" : "Ahlcon Public school"
				},
				{
				"name" : "Smith Soy",
				"age"  : "16",
				"school" : "St. Marie school"
				},
				{
				"name" : "Charle Rena",
				"age"  : "16",
				"school" : "St. Columba school"
				}
			]';
	$students = json_decode($student_json, true);
	foreach($students as $student) {
	?>
	<tr>
		<td><?php echo $student->name; ?></td>
		<td><?php echo $student->age; ?></td>
		<td><?php echo $student->school; ?></td>
	</tr>
	<?php	
		}
	?>
	</table>
 </body>
</html>

Output of the above code

Name Age School
John Garg 15 Ahlcon Public school
Smith Soy 16 St. Marie school
Charle Rena 16 St. Columba school

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