Json php格式化数据

时间:2015-01-05 12:07:27

标签: php json

我有一个像这样的json文件

{
  "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"
    }
  }
}

我需要提取每个名字的id。

我只是启动我的PHP代码,但只显示主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];
?>

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

您的JSON对象再次包含属性members members具有属性data

现在data是一个数组。

只需循环data,即可获得对象数组(成员),从中获取属性id

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

答案 2 :(得分:-3)

    $count=count($data['members']['data']);
    echo $data['id']; //echo master id
    $i=0;
    while($i<$count) {
    echo $data['members']['data'][$i]['id'];
    $i++;
    }