koruki koruki - 1 month ago 8
PHP Question

imploding array_keys from Facebook JSON data

Need some help with the sample code provided the facebook. I can't get it to return a series of IDs that I need to run a sql query against.

$friends = '{
"data": [
{
"name": "Paul",
"id": "12000"
},
{
"name": "Bonnie",
"id": "120310"
},
{
"name": "Melissa",
"id": "120944"
},
{
"name": "Simon",
"id": "125930"
},
{
"name": "Anthony",
"id": "120605"
},
{
"name": "David",
"id": "120733"
}
]
}';

$obj = json_decode($friends);
print $obj->{'data'}[0]->{'name'};


I can return the "Paul"

what I want is to return all the id's using implode(array_keys($obj),",")

I am only getting data to return.

What I'd like to do is retrieve all the IDs separated by a comma.

Thanks!

Answer

Try implode-ing on the data key with array_map:

function get_id($o) {
   return $o->id;
}

implode(array_map('get_id', $obj->data),",")