使用json_decode解析PHP中的JSON对象(for循环)

时间:2013-10-22 11:13:59

标签: php json

<?php

$articleurl = 'http://m.tonton.com.my/webservices/article/get?uniqueid=6E619732-9501-4106-860C-  A20D0016E7F0&includeChildTypes=episode&parents=package_season%2Cpackage_content%2Cprogram&c hildren=1&sortBy=published_date&sortDirection=ASC&compact=0&cache=';
$articlecontent = file_get_contents($articleurl);
$articlejson = json_decode($articlecontent, true);
$arr = $articlejson["data"]["children"];
$max = max(array_keys($arr));
echo $max; //8 episode
for ($i=0; $i<=$max;++$i)
{
    $articleid[$i] = $articlejson['data']['children']['$i']['uniqueId'];
    $mediaid[$i] = $articlejson['data']['children']['$i']['version_viostream_id']['default'];
    $resources[$i] = 'http://m.tonton.com.my/webservices/media/getProgressiveResources?articleid='.$articleid[$i].'&mediaid='.$mediaid[$i].'&token=&cache=&manifestMode=progressive';
    print_r($resources[$i]);
}

我试图找到所有可用的数组键并将可用变量插入到链接中,但是当我运行代码时,我收到错误:

  

“未定义的索引:第15行的C:\ xampp \ htdocs \ index.php中的$ i注意:   未定义的索引:第16行的C:\ xampp \ htdocs \ index.php中的$ i“

1 个答案:

答案 0 :(得分:1)

您遇到语法错误,不是'$i',而是$i

$articleurl = 'http://m.tonton.com.my/webservices/article/get?uniqueid=6E619732-9501-4106-860C-  A20D0016E7F0&includeChildTypes=episode&parents=package_season%2Cpackage_content%2Cprogram&c hildren=1&sortBy=published_date&sortDirection=ASC&compact=0&cache=';
$articlecontent = file_get_contents($articleurl);
$articlejson = json_decode($articlecontent, true);
$arr = $articlejson["data"]["children"];
$max = max(array_keys($arr));
echo $max; //8 episode
for ($i=0; $i<=$max;++$i)
{
$articleid[$i] = $articlejson['data']['children'][$i]['uniqueId'];
$mediaid[$i] = $articlejson['data']['children'][$i]['version_viostream_id']['default'];
$resources[$i] = 'http://m.tonton.com.my/webservices/media/getProgressiveResources?articleid='.$articleid[$i].'&mediaid='.$mediaid[$i].'&token=&cache=&manifestMode=progressive';
print_r($resources[$i]);
}