Matt Shirk Matt Shirk - 2 months ago 8
PHP Question

Using PHP to access elements from Facebook Graph API Array

I know this is probably a simple question, but I have been struggling with this for a few hours without any solution. I calling the Facebook Graph API and getting a result like so:

"og_object": {
"id": "1060581720684416",
"description": "It's\u00a0a chance to donate to your local shelter, volunteer with animals or simply cuddle up to your furry friend.",
"title": "National Dog Day 2016 Quotes: 15 Sayings And Photos Celebrating Man's Best Friend",
"type": "article",
"updated_time": "2016-08-26T19:26:39+0000"
"share": {
"comment_count": 0,
"share_count": 481
"id": ""

I am only interested in accessing the "share_count" and "comment_count" values from this array. Here is the PHP I am trying to use:

$searchTopFBCount = '' . $thisFBLink . '?access_token=xxxxxxxxxx';
$getthisContents = file_get_contents($searchTopFBCount);
$jsonTopFBCount = json_decode($getthisContents,true);
$jsonTopShares = $jsonTopFBCount[0][share]->share_count;
$jsonTopComments = $jsonTopFBCount[0][share]->comment_count;
$jsonTopFBTotal = $jsonTopShares + $jsonTopComments;

No matter what I try, I can't seem to get any values for
. Any help anyone can give me with this problem would be much appreciated!


As someone mentioned in the comments, I'm not sure why you're using [0] as your data isn't in a nested array. You're also trying to access the array properties like they're objects.

Try this instead:

$jsonTopShares = $jsonTopFBCount['share']['share_count'];
$jsonTopComments = $jsonTopFBCount['share']['comment_count'];