user2906713
user2906713

Reputation: 13

Parsing JSON object in PHP using json_decode(for loops)

<?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]);
}

I am trying to find all available array keys and insert the available variable into the link, but when I run the code, I get the error:

"Undefined index: $i in C:\xampp\htdocs\index.php on line 15 Notice: Undefined index: $i in C:\xampp\htdocs\index.php on line 16"

Upvotes: 1

Views: 221

Answers (1)

Legionar
Legionar

Reputation: 7597

You have a syntax error, not '$i', but $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]);
}

Upvotes: 1

Related Questions