Reputation: 187
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?
Upvotes: 1
Views: 1655
Reputation: 7722
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:
foreach ($array as $inner_array) {
echo $inner_array['name'] . "<br />";
}
Aside from that, it would be...
echo $your_array[0]['name'];
echo $your_array[1]['name'];
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 to false
otherwise. An example of usage:
$json = '{ "name": "Name",
"hometown": {
"id": "Hometown_id",
"name": "Hometown"
},
"id": "Friend_id" }';
$json_arr = json_decode($json,true);
foreach ($json_arr as $key => $var) {
if (!is_array($var)) {
if ($key == 'name') {
echo $var . "<br />";
}
}
}
Update 2:
foreach ($json_arr as $key => $var) {
if (!is_array($var)) {
if ($key == 'name') {
echo "Name: " . $var . "<br />";
}
}
else {
if ($key == 'hometown') {
echo "Hometown: " . $var['name'] . "<br />";
}
}
}
The above should do what you need.
Upvotes: 1
Reputation: 5841
Best way to work with querying specific data from Facebook is FQL. Check out https://developers.facebook.com/docs/technical-guides/fql/ and let me know if that's what you're looking for.
The sample on the link I provided will grab all of the active user's friends. If you use the sample as a starter (having filled in all the proper info for your app along with a valid access token), then go to the area where it says // run fql query
and change uid2
to name
in the following line:
fql?q=SELECT+uid2+FROM+friend+WHERE+uid1=me()
Adding the following to the end of the script would then populate all of the friends' names into the array called $names
$r = $fql_multiquery_obj['data'][0]['fql_result_set'];
foreach ($r as $u)
{
$name[count($name)] = $u['name'];
}
Does that help you do what you're trying to do? Let me know :)
Upvotes: 0