0

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!

1 Answer 1

1

Try implode-ing on the data key with array_map:

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

implode(array_map('get_id', $obj->data),",")
Sign up to request clarification or add additional context in comments.

1 Comment

Hi can you also let me know how I could return the index by ID, I want to use it to return the name string by ID. Thanks, I used array_search but it kept returning null

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.