Im trying to return the friend list of a user using a Facebook app without using the PHP SDK.
I have managed to return the users name, gender, etc. without using an auth code. Im now trying to return a list of names who are the users friends, all ive managed to do so far is print the array, I cant seem to only print the name as its laid out differently to the users name, gender, etc. which I managed to echo by doing:
$user->name
But when I try that for the friends list it doesnt work and doesnt echo anything at all, I was just wondering if anyone knew how I go about doing this?
Using print_r I get the following:
Array(
[0] => Array ( [name] => Friend's name [id] => x )
[1] => Array ( [name] => Friend's name [id] => x )
[2] =>....)
Until all of the friends have been listed.
{ "name": "Name",
"hometown": {
"id": "Hometown_id",
"name": "Hometown"
},
"id": "Friend_id" }
Above is the structure of the array of each, I think a potential issue is that not everybody has a hometown set which may be why I’m receiving a forEach error?
Given that array structure, it’s pretty simple(in php) to get what you want. I recommend reading from php’s manual on arrays.
Just try something like this:
Aside from that, it would be…
And so on and so fourth..
Update:
What you are now looking is not an array, but a javascript object. It is referred to as JSON: Javascript Object Notation.
You need a special parser to interpret it as an array:
json_decode(), which comes shipped with php greater than or equal to 5.2.0. To make the array associative, you must pass a second parameter, a boolean,true. It defaults tofalseotherwise. An example of usage:Update 2:
The above should do what you need.