-1

When I run following code:

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

I get output as following:

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

The question I have is how do I access various elements here including how many total sets are there and in each set access a particular object.

Thanks for your help in advance.

Qirel
  • 25,449
  • 7
  • 45
  • 62
  • Read the `json_decode()` [manual page](http://php.net/manual/en/function.json-decode.php) or almost any other question tagged with `json` – RiggsFolly Nov 14 '16 at 00:26
  • Figured out - this post did the magic https://stackoverflow.com/questions/30430454/how-to-loop-over-and-access-various-elements-in-an-array-that-is-both-multidimen?rq=1 – Gurmeet Singh Nov 14 '16 at 00:35

2 Answers2

0

Why are you decoding the output and after that you are encoding it again? Look what json_decode does: http://php.net/manual/de/function.json-decode.php

The decoding of your output seems to work well.

So just do the following instead:

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

You will probably get an PHP array containing objects. On this array you can apply anything which is possible with arrays in PHP. You can use count() for instance to count the number of results in the array.

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

Blackbam
  • 17,496
  • 26
  • 97
  • 150
0

$json2 is a associative array, access is done using $json[0]["stream"] (for example).

To access all elements, try

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"];
  }
}

(See also Parsing JSON object in PHP using json_decode)

Community
  • 1
  • 1
camelCase
  • 187
  • 1
  • 12