How to Parse Entire Json into Array 

student-2.json


{

"student":[{

"rollno":"1001",

"name":"awnish",

"class":"12th",

"passed":true,

"marks":60

},

{

"rollno":"1002",

"name":"ravi",

"class":"11th",

"passed":false,

"marks":-10


},

{

"rollno":"1003",

"name":"mansa",

"class":"L.k.g",

"passed":"Not this Birth",

"marks":"-infinity"


},

{

"rollno":"1004",

"name":"Dubey",

"class":"Class Key Peechey",

"passed":"Bhool Jao",

"marks":"Tumharey Bas Ka Nahi"


}]

}



parse.php


 


<?php


//How to Parse Entire Json into Array


$json_str=file_get_contents("student-2.json");//Json_string

$json_arr=json_decode($json_str,true);


#var_dump($json_arr);

#echo "<hr/>";

//exit;


foreach($json_arr['student'] as $student):

echo "<hr/>";

echo "<b>Student data :</b>";

echo "<hr/>";

echo "Student Rollno : {$student['rollno']} <br/>";

echo "Student Name : {$student['name']} <br/>";

echo "Student class : {$student['class']} <br/>";

echo "Student passed : {$student['passed'] } <br/>";

echo "Student Marks : {$student['marks'] } <br/>";

endforeach;


echo "<hr/>";

echo "<h3>Find the Student whose rollno =1003</h3>";


foreach($json_arr['student'] as $student):

if($student['rollno']==1003):

echo "<hr/>";

echo "<b>Student data :</b>";

echo "<hr/>";

echo "Student Rollno : {$student['rollno']} <br/>";

echo "Student Name : {$student['name']} <br/>";

echo "Student class : {$student['class']} <br/>";

echo "Student passed : {$student['passed'] } <br/>";

echo "Student Marks : {$student['marks'] } <br/>";

endif;

endforeach;