<?php
$id = '2422414574';
$json = file_get_contents("http://xxxxxx.com/api.php?token=xxxx&id=xxxx");
$data = json_decode($json);
echo $data->phim[0]->filmName . "<br/>";
echo $data->phim[0]->epsList[0]->name . " - ";
echo $data->phim[0]->epsList[0]->id . "<br/>";
echo $data->phim[0]->epsList[1]->name . " - ";
echo $data->phim[0]->epsList[1]->id . "<br/>";
echo $data->phim[0]->epsList[2]->name . " - ";
echo $data->phim[0]->epsList[2]->id . "<br/>";
echo $data->phim[0]->epsList[3]->name . " - ";
echo $data->phim[0]->epsList[3]->id . "<br/>";
echo $data->phim[0]->epsList[4]->name . " - ";
echo $data->phim[0]->epsList[4]->id . "<br/>";
echo $data->phim[0]->epsList[5]->name . " - ";
echo $data->phim[0]->epsList[5]->id . "<br/>";
echo $data->phim[0]->epsList[6]->name . " - ";
echo $data->phim[0]->epsList[6]->id . "<br/>";
echo $data->phim[0]->epsList[7]->name . " - ";
echo $data->phim[0]->epsList[7]->id . "<br/>";
echo $data->phim[0]->epsList[xxxx]->name . " - ";
echo $data->phim[0]->epsList[xxxx]->id . "<br/>";
echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
如您所见,我必须多次重复1个代码。有没有重复代码可以做到这一点的简短方法? 例如,我想从this URL获取来自 id 和名称的所有值,其中包含24个项目。这意味着我必须重复24次代码:(
答案 0 :(得分:1)
foreach()怎么样?
foreach($data->phim as $phim)
{
echo $phim->filmName . "<br/>";
foreach($phim->epsList as $epsList)
{
echo $epsList->name . " - ";
echo $epsList->id . "<br/>";
}
}
答案 1 :(得分:1)
您需要使用foreach循环迭代它。这是PHP的基本功能之一,有很多教程。