繁体   English   中英

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

[英]how do i access nested arrays from json array and restrict for loop to loop for 2 times

我是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...

在计算count($item->draws[$i])时, $i是未定义的。 它应该是

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

为什么您根本不访问您的第一个和第二个draws数组元素,如下所示:

foreach($myItem as $item) {
    echo '<div class="mydiv">
    '.$item->draws[0]->id.'
    </div>';
    echo '<div class="mydiv">
    '.$item->draws[1]->id.'
    </div>';
}
$decodedJson = json_decode($json, true);

foreach ($decodedJson['items'] as $item) {

    $draws = array_column($item['draws'], 'id');

    echo implode(' ', array_slice($draws, 0, 2)) . '<br />';
}

输出为:

26202 26094
24110 24122
26848 19104
31116 31138
31144 22890

这是演示

尝试这个:

<div class="container">
   <?php
      $json = json_decode($line);
      $myItem = $json->items;
      foreach($myItem as $item) {
      for ($i=0; $i<count($item->draws[$i]); $i++) {
      ?>
   <div class="mydiv">
      <?php
         for ($k=0; $k < 2; $k++) {

             $id=$item->draws[$k]->id;
         ?>
      <?php echo $id;
         }?>
   </div>
   <?php }}?>
</div>

我得到这种类型的输出:

26202 26094
24110 24122
26848 19104
31116 31138
31144 22890

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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