2016-11-14 190 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