How to loop over a json object in PHP

1 Answer

0 votes
$student_json = '[
		{
			"name" : "abc",
			"age"  : "24",
			"school" : "PHP school"
		},
		{
			"name" : "xyz",
			"age"  : "27",
			"school" : "Java school"
		},
		{
			"name" : "uvw",
			"age"  : "22",
			"school" : "C school"
		}
	]';

$students = json_decode($student_json, true);

foreach ($students as $student) {
    print_r($student);
	echo $student['name']. "\n";
    echo $student['age']. "\n";
	echo $student['school'] . "\n\n";
}




/*
run:

Array
(
    [name] => abc
    [age] => 24
    [school] => PHP school
)
abc
24
PHP school

Array
(
    [name] => xyz
    [age] => 27
    [school] => Java school
)
xyz
27
Java school

Array
(
    [name] => uvw
    [age] => 22
    [school] => C school
)
uvw
22
C school

*/

 



answered Jan 29, 2022 by avibootz

Related questions

1 answer 164 views
164 views asked Feb 16, 2021 by avibootz
1 answer 190 views
1 answer 240 views
3 answers 337 views
2 answers 123 views
123 views asked May 25, 2022 by avibootz
2 answers 245 views
...