【问题标题】:how do i access nested arrays from json array and restrict for loop to loop for 2 times我如何从 json 数组访问嵌套数组并限制 for 循环循环 2 次
【发布时间】: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


【解决方案1】:

$i 在您计算 count($item-&gt;draws[$i]) 时未定义。应该是

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

【讨论】:

  • 这很好,但它给了我所有的平局,但我想要前 2 个结果,我在上面给出了预期输出的清晰图片。
【解决方案2】:

为什么你根本不访问你的第一个和第二个绘制数组元素如下:

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

【讨论】:

    【解决方案3】:
    $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
    

    这里是the demo

    【讨论】:

    • @noushadmohammed 如果你觉得它有用,你也必须接受一个答案
    【解决方案4】:

    试试这个:

    <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
    

    【讨论】:

    • 我得到了预期的输出,但出现以下错误。注意:未定义的偏移量:第 34 行的 C:\wamp\www\json\test.php 中的 1 注意:未定义的偏移量:第 31 行的 C:\wamp\www\json\test.php 中的 1 您能帮我吗?
    • 注意:尝试在第 34 行获取 C:\wamp\www\json\test.php 中非对象的属性
    • 您在问题中提供的 JSON 字符串无效。请在 json.parser.online.fr/.I 中检查您的 JSON,自行纠正
    • 在这里查看我的代码。eval.in/795205。它不会引发任何类型的错误或警告。
    • 我正在使用 jsonlint 作为 api 结果,并且该 api 是由优秀的团队编写的。
    猜你喜欢
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 2021-12-15
    • 2017-09-03
    • 1970-01-01
    相关资源
    最近更新 更多