2016-11-14 142 views
-1

当我运行下面的代码:如何访问元素嵌套数组中的PHP

$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, 
........... 
}] 

我的问题是怎样访问各种元素这里包括有多少个总集,并在每个集中访问一个特定的对象。

感谢您的帮助提前。

+0

阅读'json_decode()'[手册](http://php.net/manual/en/function .json-decode.php)或几乎所有其他问题标记'json' – RiggsFolly

+0

想通了 - 这篇文章做了魔术https://stackoverflow.com/questions/30430454/how-to-loop-over-and-access-各种元素合的阵列 - 这此结果既-multidimen?RQ = 1 –

回答

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

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