2

I have a json file like this

{
  "id": "123456789012345", 
  "members": {
    "data": [
      {
        "name": "Dylan John", 
        "administrator": false, 
        "id": "12341234"
      }, 
      {
        "name": "Test user", 
        "administrator": false, 
        "id": "43214321"
      }, 
      {
        "name": "Demo user", 
        "administrator": false, 
        "id": "55445544"
      }, 
      {
        "name": "Fake user", 
        "administrator": false, 
        "id": "11991199"
      }
    ], 
    "paging": {
      "next": "www.123456demo12345.com"
    }
  }
}

I need to extract the id of each name.

I just start my php code like that but simply display only the master id:

<?php
$url = "http://www.1234demo1234fake.com/user.json";

$json = file_get_contents($url);
$data = json_decode($json, TRUE);

echo $data[id]; //echo master id
echo $data[members][data][id];
?>
1
  • Use quotes around key names like echo $data['id'], because otherwise you try to use constants values (that is not set) Commented Jan 5, 2015 at 12:10

2 Answers 2

1

You have to iterate over $data['members']['data'] and print $data['members']['data'][$i]['id'].

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

Comments

1

Your JSON object contains property members again members has property data.

Now data is an array.

Just loop over data, you get array of objects (members), fetch property id from it.

<?php
$abc = json_decode($your_json_sting);
$membersData = $abc->members->data;
$memberIds = array();
foreach ($membersData as $mem) {
    $memberIds[] = $mem->id;
}
echo '<pre>';print_r($memberIds);echo '</pre>';
?>

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.