I need to extract below fields from JSON, cant able to traverse some objects.
jSON (https://api.myjson.com/bins/19l74k)
Here is loop code
$results = json_decode($json,true);
foreach($results['data'] as $res){
echo $res['message']."<br/><br/>";
echo $res['shares']."<br/><br/>";
echo "<hr/>";
}
Need to extract Shares Count, Comments(total_count),reactions(total_count). I tried looping shares with ['data'] but undefined index issue.
There is an array in the data as well that needs to be accounted for -
Perhaps this will help
<?php
$f=file_get_contents("https://api.myjson.com/bins/19l74k");
$dataArray=json_decode($f,true);
$dataObject=json_decode($f);
print("By array reference: ".$dataArray['data'][0]['shares']['count']."
");
print("By object reference: ".$dataObject->data[0]->shares->count."
");
?>
Try this one,
$data = '{"data":[{"id":"123","created_time":"2018-10-04T11:51:28+0000","link":"link","picture":"picture","message":"message","shares":{"count":2},"comments":{"data":[],"summary":{"order":"ranked","total_count":0,"can_comment":true}},"reactions":{"data":[],"summary":{"total_count":37,"viewer_reaction":"NONE"}}},{"id":"2918","created_time":"2018-10-04T11:47:36+0000","link":"hss","picture":"aasd","message":"asdsa","shares":{"count":14},"comments":{"data":[],"summary":{"order":"ranked","total_count":0,"can_comment":true}},"reactions":{"data":[],"summary":{"total_count":227,"viewer_reaction":"NONE"}}}]}';
$output = json_decode($data, TRUE);
foreach ($output['data'] as $key => $value) {
echo $value['message'].'<br/>';
echo $value['shares']['count'].'<br/>';
}