doyhq66282 2017-05-15 08:17
浏览 87

我如何从json数组访问嵌套数组并限制循环循环2次

I am new to Json and php. I found many answers in stack overflow but none of them is matching my question.

following is the json result from the browser

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

this is my code :

$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>

above code gives me the error. now my question is how can i display first 2 rows of each draws that is

expected output :

26202 26094
24110 24122
26848 19104
goes on...
  • 写回答

4条回答 默认 最新

  • dongyan0629 2017-05-15 08:26
    关注

    $i is undefined at the time when you calculate count($item->draws[$i]). It should be

    for ($i=0; $i<count($item->draws); $i++) {
    
    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私