Hello I have Json like this:
[{
"exam_code": "1",
"name": "Name1",
"surname": "Surname1",
"father_name": "Fname1",
"id_number": "211111",
"district_number": "21",
"school_number": "025",
"gender": "F",
"variant": "A",
"grade": "4",
"sector": "A",
"foreign_language": "I",
"answerList": {
"gradeFour": {
"lesson1": ["A", "C", "C", "C", "A", "A", "B", "B", " ", "C", "C", "B", "A", "C", "C", "B", "B", "C", "B", "A"],
"lesson2": ["B", "A", " ", "C", " ", "B", " ", "B", "B", "C", " ", " ", "B", "A", "A", "A", "C", "A", "B", "B"],
"lesson3": ["A", "C", "B", "B", "A", "A", "C", "A", "C", "C"],
"lesson4": ["B", "B", "A", "B", "B"],
"lesson5": ["B", "A", "A", "B", "B"],
"lesson6": ["B", "A", "A", "B", "A", "B", "A", "A", "C", "B"]
}
}
}]
I am trying to print out lessons answers on foreach loop.
tried below code:
<?php
$msc = microtime(true);
$array = (json_decode($raw_str,true));
foreach($array as $value){
echo $value['id_number'];
echo '<br/>';
foreach($value -> answerList->gradeFour as $val){
echo $val;
}
echo '<br>---------------------------';
echo '<br>';
}
$msc = microtime(true)-$msc;
echo ($msc * 1000) . ' ms'; // in millseconds
?>
But getting this error
Notice: Trying to get property of non-object in G:\xampp\htdocs\siec\string_test.php on line 50 Notice: Trying to get property of non-object in G:\xampp\htdocs\siec\string_test.php on line 50 Warning: Invalid argument supplied for foreach() in G:\xampp\htdocs\siec\string_test.php on line 50
$value['id_number']and then switch to object notation access($value -> answerList), you can use one or the other (depending on the second argument tojson_decode()) but can't mix them.