<?php
$json = <<<JSON
{
"quiz": {
"sport": {
"q1": {
"question": "Which one is correct team name in NBA?",
"options": [
"New York Bulls",
"Los Angeles Kings",
"Golden State Warriros",
"Huston Rocket"
],
"answer": "Huston Rocket"
}
},
"maths": {
"q1": {
"question": "5 + 7 = ?",
"options": [
"10",
"11",
"12",
"13"
],
"answer": "12"
},
"q2": {
"question": "12 - 8 = ?",
"options": [
"1",
"2",
"3",
"4"
],
"answer": "4"
}
}
}
}
JSON;
$jsonFile = json_decode($json);
function getMathQuestions($jsonFile) {
foreach($jsonFile as $val) {
if(is_object($val)) {
getMathQuestions($val);
} else {
if(is_array($val)) {
getMathQuestions($val);
} else {
echo $val . "<br>";
}
}
}
}
echo getMathQuestions($jsonFile);
preferences:
27.53 ms | 404 KiB | 5 Q