Reputation: 599
Ok I've been trying for days now to get somewhere with this so please could someone help me out with an answer that actually works? I've been pouring through the JSON data you get from the Facebook API via the address https://graph.facebook.com/search?q=home&access_token=... and I know for a fact that there are likes (not with every post) but I cannot for the life of me get an actual count. The JSON output for likes goes into a seperate array as follows
"likes": {
"data": [
{
"id": "123456789010",
"name": "joe bloggs"
},
and so on... until there aren't any more likes and then it goes onto the next post. I am having a hellish time trying to get a count of how many ids are in each like->data array. I've tried the following:
foreach ($item->likes->data as $like) {
$likecount = count($like->id);
}
but this doesn't work and putting another foreach() inside the first one doesn't work either, neither the count() nor the sizeof() functions seem to do the trick at all and I've even tried putting a for() loop inside as well and still can't get it to work.
Could someone please tell me where I'm going wrong, for example recreate the multi-dimen array and try the foreach() loops yourself and see where I'm going wrong with this.
Thank you in advance for any help, much appreciated!
Upvotes: 0
Views: 88
Reputation: 1512
If you are trying to get just a count of how many likes there are, then you can do this:
foreach ($items as $item) {
$total_likes = count($item->likes->data)
// do stuff with the total
}
If you are trying to get a list of like ids, do this:
foreach (items as $item) {
$item_like_ids = array();
foreach ($item->like->data as $like)
$item_like_ids[] = $like->id;
// do stuff with the array of ids
}
Hope this helps.
Upvotes: 1