2

I am trying to get facebook friends by using facebook api.

I am getting response

   {
  "data": [
  {
     "id": "groupID",
     "members": {
        "data": [
           {
              "name": "Abc",
              "administrator": false,
              "id": "xxxxxx"
           },
           {
              "name": "NewCas",
              "administrator": false,
              "id": "xxxxxxxxx"
           },
           {
              "name": "Cds",
              "administrator": false,
              "id": "xxxxxxxxx"
           },
           {
              "name": "akaha",
              "administrator": false,
              "id": "xxxxxxx"
           },

  }

}

This is my code

$fql = 'https://graph.facebook.com/me/groups?fields=id,members&access_token='.$access_token.'&limit=3';
$fqlresult = file_get_contents($fql); 
$f = json_decode($fqlresult, true);

tried implode.

$result = implode(',', array_column($f['data'], 'id'));

I am getting this response

GroupID,GroupID,GroupID

I want to take response user ids (members id) as

xxxxxx,xxxxxx,xxxxxx,xxxxxx,xxxxxx,xxxxxx,xxxxxx

Thanks

Vinay
  • 57
  • 1
  • 11

5 Answers5

1

The other answers are almost correct, but the data is an array with one element so it should be:

echo implode(',', array_column($f['data'][0]['members']['data'], 'id'));

and that is when you have only one groupid, if you have multiple group ids you will need to loop trough it. (loop over the [0] by checking $groupcount = count($f['data']);

Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
0

This works...

$arr = array(
    'data' => array(
        array('id' => 'xxxx1'),
        array('id' => 'xxxx2'),
        array('id' => 'xxxx3'),
        array('id' => 'xxxx4'),
        array('id' => 'xxxx5'),
        array('id' => 'xxxx6'),
        array('id' => 'xxxx7'),
        array('id' => 'xxxx8'),
    )
);

echo implode(',', array_column($arr['data'], 'id'));

EDIT - based on your update and change of request...

echo implode(',', array_column($arr['data'][0]['members']['data'], 'id'));

please review http://php.net/manual/en/language.types.array.php and the section on multidimensional arrays

Barry
  • 3,303
  • 7
  • 23
  • 42
  • Hello sir I just changed my code a bit for getting group members. – Vinay May 18 '16 at 09:32
  • well I didn't test it the same way as I don't have the exact object setup, but there was some independent thought assumed on your part by reading and understanding multi dimensional arrays. I'll make one final edit and then if it works, I'd appreciate this being selected, otherwise I'll leave this question as I normally only 3 changes on SO :) – Barry May 18 '16 at 10:05
0
echo implode(',', array_column($f['data'][0]['members']['data'], 'id'));
oxmolol
  • 125
  • 1
  • 1
  • 10
0
$data_arr=json_decode($data,true);
$return_str="";
foreach($data_arr['data'] as $row)
{
    $return_str .=implode(", ", array_column($row['members']['data'], 'id'))  ;  
}
echo rtrim($return_str,",");

This will work for multiple elements in $f['data'].

Naisa purushotham
  • 905
  • 10
  • 18
0

Make sure your PHP version is 5.5+. Below the specified version will not support array_column function. You can also use this code without array_column.

$data = $result['data'][0]['members']['data'];
$keyValue = '';

foreach ($data as $outerKey => $outerValue) {
  foreach ($outerValue as $key => $value) {
    if($key == 'id'){
      $keyValue .= $value . ' ';
    }
  }
}
echo $keyValue;