I have a json data from API and i want to insert them to my database table. I have extract $data
using json_encode
function, but when i tried to access data inside $myJson
with some of this code, it gives me an error result.
$data = '{"posts":[{"post":{"math_score":"85","history_score":"70"}}]}';
$myJson = json_decode($data);
foreach ($myJson as $mj){
echo $mj['math_score'];
// echo $mj->math_score; <= error
// echo $mj[0]->post->math_score; <= error
// echo $mj->post->math_score; <= error
}
The error :
Invalid argument supplied for foreach()
.
sorry for my bad grammar, any answer will be greatly appreciated. Thanks