Vinay Vinay - 6 months ago 17
PHP Question

convert php response array as comma separated

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

Answer

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']);

Comments