如何在php中访问嵌套数组中的元素

时间:2016-11-14 00:09:55

标签: php json multidimensional-array

当我运行以下代码时:

$json2=json_decode($output, true);
echo "<pre>";
echo json_encode($json2, JSON_PRETTY_PRINT);
echo "</pre>";

我输出如下:

[
    {
        "stream": "live\/cm1",
        "size": 1120438999,
        "duration": 16233,
        "periods": 7,
        "path": "\/var\/cache\/nimble\/dvr\/live\/cm1",
        "space_available": 212516282368,
        "vcodec": "avc1.4d001f",
        "resolution": "1280x720",
        "bandwidth": 552176,
        "timeline": [
            {
                "start": 1477757618,
                "duration": 535
            },
            {
                "start": 1477758226,
                "duration": 703
            },
            {
                "start": 1477760431,
                "duration": 14295
            },
            {
                "start": 1477829472,
                "duration": 559
            },
            {
                "start": 1477879433,
                "duration": 40
            },
            {
                "start": 1477881429,
                "duration": 79
            },
            {
                "start": 1477881925,
                "duration": 22
            }
        ]
    },
    {
        "stream": "live\/cm10",
        "size": 1790211828,
        "duration": 33976,
        "periods": 23,
        "path": "\/var\/cache\/nimble\/dvr\/live\/cm10",
        "space_available": 212516282368,
        "vcodec": "avc1.4d001f",
        "resolution": "1280x720",
        "bandwidth": 421520,
        "timeline": [
            {
                "start": 1477757606,
                "duration": 193
            },
            {
                "start": 1477757817,
                "duration": 336
            },
            {
                "start": 1477758226,
                "duration": 703
            },
            {
                "start": 1477759027,
                "duration": 1378
            },
            {
                "start": 1477760460,
                "duration": 14273
            },
            {
                "start": 1477829464,
                "duration": 1235
            },
            {
                "start": 1477878029,
                "duration": 469
            },
            {
                "start": 1477882600,
                "duration": 1260
            },
            {
                "start": 1477883914,
                "duration": 208
            },
            {
                "start": 1477911214,
                "duration": 1528
            },
            {
                "start": 1477913185,
                "duration": 185
            },
            {
                "start": 1477913546,
                "duration": 759
            },
            {
                "start": 1477915819,
                "duration": 68
            },
            {
                "start": 1478782219,
                "duration": 69
            },
            {
                "start": 1478782375,
                "duration": 76
            },
            {
                "start": 1478812920,
                "duration": 659
            },
            {
                "start": 1478911726,
                "duration": 2334
            },
            {
                "start": 1478914138,
                "duration": 5
            },
            {
                "start": 1478914233,
                "duration": 2872
            },
            {
                "start": 1478917165,
                "duration": 1133
            },
            {
                "start": 1478976623,
                "duration": 224
            },
            {
                "start": 1478981537,
                "duration": 3658
            },
            {
                "start": 1479065575,
                "duration": 351
            }
        ]
    },
    {
        "stream": "live\/cm2",
        "size": 1320002727,
        "duration": 20809,
        "periods": 13,
...........
}]

我遇到的问题是如何访问此处的各种元素,包括总共有多少个集合以及每个集合访问特定对象。

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

为什么要对输出进行解码,然后再对其进行编码?看看json_decode的作用:http://php.net/manual/de/function.json-decode.php

输出的解码似乎运作良好。

所以,请执行以下操作:

$json2=json_decode($output, true);
echo "<pre>";
var_dump($json2);
echo "</pre>";

您可能会得到一个包含对象的PHP数组。在这个数组上,你可以在PHP中应用任何可能的数组。例如,您可以使用count()来计算数组中的结果数。

http://php.net/manual/en/language.types.array.php

答案 1 :(得分:0)

$json2是一个关联数组,使用$json[0]["stream"](例如)进行访问。

要访问所有元素,请尝试

foreach( $json2 as $item ) {
  echo $item["stream"] . " has a size of " . $item["size"] . " bytes";

  foreach( $item["timeline"] as $timeline ) {
    //access the timeline-elements (in the case you actually need those)
    echo $timeline["start"];
  }
}

(另见Parsing JSON object in PHP using json_decode