简体   繁体   中英

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

您必须遍历$data['members']['data']并打印$data['members']['data'][$i]['id']

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>';
?>
    $count=count($data['members']['data']);
    echo $data['id']; //echo master id
    $i=0;
    while($i<$count) {
    echo $data['members']['data'][$i]['id'];
    $i++;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM