Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

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

2 answers 102 views
102 views asked May 25, 2022 by avibootz
1 answer 143 views
143 views asked Feb 16, 2021 by avibootz
1 answer 158 views
1 answer 224 views
3 answers 305 views
2 answers 216 views
...