0

i need to get data from array_keys the script i use in the server side:

PHP:

$friends = json_decode(file_get_contents(
'https://graph.facebook.com/me/friends?access_token=' .
   $facebook->getAccessToken() ), true);
$friend_ids = array_keys($friends);

the data of array look as above:

{
   "data": [
      {
         "name": "Tal Rozner",
         "id": "554089741"
      },
      {
         "name": "Daniel Kagan",
         "id": "559274789"
      },
  {
         "name": "ron cohen",
         "id": "100001553261234"
      }
   ]
}

i need to get all this data to an array that i can work with it.

how can i do it ? tanks,

2
  • I don't understand. Based in the JSON data above, which values do you want? Commented Sep 29, 2010 at 16:26
  • What's preventing you from working with the array in its current form? What specifically are you trying to do with the array? Commented Sep 29, 2010 at 16:31

2 Answers 2

1

If i understand your question correctly (and I am not sure that I do) you might want something like

$by_id = array();
foreach ($friends['data'] as $item) {
    $by_id[ $item['id'] ] = $item['name'];
}

Which will give you and array that looks like this:

print_r ($by_id);

Array
(
    [554089741] => Tal Rozner
    [559274789] => Daniel Kagan
    [100001553261234] => ron cohen
)

Which might be easier for you to work with...

Sign up to request clarification or add additional context in comments.

Comments

0

Not sure what you mean "work with it." If the JSON response from Facebook is what you posted, you should be able to do this:

foreach ($friends['data'] as $friend) {
    echo "ID: {$friend['id']}" . PHP_EOL;
    echo "ID: {$friend['name']}" . PHP_EOL;
    echo PHP_EOL;
}

This would produce:

ID: 554089741
Name: Tal Rozner

ID: 559274789
Name: Daniel Kagan

ID: 100001553261234
Name: ron cohen

The $friends var would already be an array due to your use of json_decode(). In this case array_keys() isn't needed, and would only produce array (0, 1, 2).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.