Alessandro Giani
Alessandro Giani

Reputation: 21

Json php formatted data

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];
?>

Upvotes: 2

Views: 73

Answers (2)

Pupil
Pupil

Reputation: 23948

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>';
?>

Upvotes: 1

Œlrim
Œlrim

Reputation: 545

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

Upvotes: 1

Related Questions