【发布时间】:2017-05-15 08:17:08
【问题描述】:
我是 Json 和 php 的新手。我在堆栈溢出中找到了很多答案,但没有一个与我的问题相匹配。
以下是浏览器的json结果
{
"meta": {
"total_pages": 350,
"total_records": 6988
},
"items": [{
"id": 5972,
"name": "Sunfoil 3-Day Cup",
"verification_level": 10,
"avatar": "https://da57fee7585ze.cloudfront.net/assets/original/competition/default_profile.png",
"published_at": "2015-11-14T09:14:01Z",
"organisation": {
"id": 241,
"name": "Cricket South Africa"
},
"draws": [{
"id": 26202,
"name": "Sunfoil 3-day Cup, 2016/17 (Cross Pool)",
"season": "2016/17",
"season_id": 260,
"published_at": "2017-01-02T09:28:11Z",
"archived_at": null,
"match_level_id": 13
}, {
"id": 26094,
"name": "Sunfoil 3-day Cup, 2016/17 (Pool A, B & Final)",
"season": "2016/17",
"season_id": 260,
"published_at": "2016-11-23T04:44:43Z",
"archived_at": null,
"match_level_id": 13
}, {
"id": 19052,
"name": "Sunfoil 3-day Cup, 2015/16 (Pool A, B & Final)",
"season": "2015/16",
"season_id": 258,
"published_at": "2015-11-14T09:14:01Z",
"archived_at": "2016-09-26T03:59:52Z",
"match_level_id": 13
}, {
"id": 19090,
"name": "Sunfoil 3-day Cup, 2015/16 (Cross Pool)",
"season": "2015/16",
"season_id": 258,
"published_at": "2015-11-14T09:14:01Z",
"archived_at": "2016-09-27T06:37:03Z",
"match_level_id": 13
}, {
"id": 11580,
"name": "Sunfoil 3-Day Cup, 2014/15",
"season": "2014/15",
"season_id": 256,
"published_at": "2015-11-14T09:14:01Z",
"archived_at": "2015-09-25T12:09:56Z",
"match_level_id": 13
}],
"entity_type": "Competition",
"followed_by_user?": false,
"entity_is_user?": false
}, {
"id": 7219,
"name": "CSA Provincial One-Day Challenge",
"verification_level": 10,
"avatar": "https://da57fee7585ze.cloudfront.net/assets/original/competition/default_profile.png",
"published_at": "2015-09-28T11:58:01Z",
"organisation": {
"id": 241,
"name": "Cricket South Africa"
},
"draws": [{
"id": 24110,
"name": "CSA Provincial One-Day Challenge 2016/17 ( Pool A,B & Final )",
"season": "2016/17",
"season_id": 260,
"published_at": "2016-11-26T10:17:54Z",
"archived_at": null,
"match_level_id": 13
}, {
"id": 24122,
"name": "CSA Provincial One-Day Challenge 2016/17(Cross Pool)",
"season": "2016/17",
"season_id": 260,
"published_at": "2016-12-03T11:42:05Z",
"archived_at": null,
"match_level_id": 13
}, {
"id": 19113,
"name": "CSA Provincial One-Day Challenge 2015/16",
"season": "2015/16",
"season_id": 258,
"published_at": "2016-04-11T04:41:07Z",
"archived_at": "2016-06-21T04:43:05Z",
"match_level_id": 13
}],
"entity_type": "Competition",
"followed_by_user?": false,
"entity_is_user?": false
}, {
"id": 5890,
"name": "Momentum One-Day Cup",
"verification_level": 10,
"avatar": "https://dnd3y8e5nonx2.cloudfront.net/competitions/avatars/5890/1441435021/profile.png",
"published_at": "2015-04-01T10:46:19Z",
"organisation": {
"id": 241,
"name": "Cricket South Africa"
},
"draws": [{
"id": 26848,
"name": "Momentum One-Day Cup 2016/17",
"season": "2016/17",
"season_id": 260,
"published_at": "2016-10-17T06:56:48Z",
"archived_at": null,
"match_level_id": 13
}, {
"id": 19104,
"name": "Momentum One-Day Cup 2015/16",
"season": "2015/16",
"season_id": 258,
"published_at": "2016-02-12T06:33:37Z",
"archived_at": "2016-09-28T04:52:58Z",
"match_level_id": 13
}, {
"id": 11422,
"name": "Momentum One-Day Cup 2014/15",
"season": "2014/15",
"season_id": 256,
"published_at": "2016-11-08T09:37:30Z",
"archived_at": "2015-09-28T09:38:21Z",
"match_level_id": 13
}, {
"id": 4219,
"name": "Momentum One Day Cup 2013/14",
"season": "2013/14",
"season_id": 254,
"published_at": "2013-09-30T11:56:45Z",
"archived_at": "2015-03-30T11:28:46Z",
"match_level_id": 13
}, {
"id": 3822,
"name": "Momentum One Day Cup, 2012/13",
"season": "2012/13",
"season_id": 252,
"published_at": "2013-08-26T10:06:41Z",
"archived_at": "2015-03-30T11:28:35Z",
"match_level_id": 13
}],
"entity_type": "Competition",
"followed_by_user?": false,
"entity_is_user?": false
}, {
"id": 10291,
"name": "Papua New Guinea Tour of United Arab Emirates 2017",
"verification_level": 10,
"avatar": "https://da57fee7585ze.cloudfront.net/assets/original/competition/default_profile.png",
"published_at": "2017-04-02T06:20:09Z",
"organisation": {
"id": 13,
"name": "ICC"
},
"draws": [{
"id": 31116,
"name": "T20I Series",
"season": "2017",
"season_id": 261,
"published_at": "2017-04-02T06:20:09Z",
"archived_at": null,
"match_level_id": 14
}, {
"id": 31138,
"name": "Only ODI",
"season": "2017",
"season_id": 261,
"published_at": "2017-04-04T05:26:22Z",
"archived_at": null,
"match_level_id": 14
}],
"entity_type": "Competition",
"followed_by_user?": false,
"entity_is_user?": false
}, {
"id": 8179,
"name": "Pakistan Cup",
"verification_level": 10,
"avatar": "https://dnd3y8e5nonx2.cloudfront.net/competitions/avatars/8179/1461040967/profile.jpeg",
"published_at": "2017-04-04T06:26:24Z",
"organisation": {
"id": 446,
"name": "Pakistan Cricket Board"
},
"draws": [{
"id": 31144,
"name": "Pakistan Cup 2017",
"season": "2017",
"season_id": 261,
"published_at": "2017-04-19T06:18:41Z",
"archived_at": null,
"match_level_id": 13
}, {
"id": 22890,
"name": "Pakistan Cup 2016",
"season": "2016",
"season_id": 259,
"published_at": "2017-04-04T06:26:24Z",
"archived_at": "2017-04-04T06:28:32Z",
"match_level_id": 13
}],
"entity_type": "Competition",
"followed_by_user?": false,
"entity_is_user?": false
},
这是我的代码:
$url="aaaaaa";
$content = file_get_contents($url);
$json = json_decode($content);
?>
<div class="container">
<?php $myItem = $json->items;
foreach($myItem as $item) {
for ($i=0; $i<count($item->draws[$i]); $i++) {
$id=$item->draws[$i]->id; ?>
<div class="mydiv">
<?php echo $id;?>
</div>
<?php }}?>
</div>
上面的代码给了我错误。现在我的问题是如何显示每个抽奖的前 2 行
预期输出:
26202 26094
24110 24122
26848 19104
goes on...
【问题讨论】:
-
你的 json 不是一个有效的...仔细检查输出的 json...
标签: php arrays json foreach nested-loops