首页 > 解决方案 > PHP with two FOR loops

问题描述

I have two "for" loops

First for is to get year range between 1990 and 2019, and the for inside it have to run my script five times with the same year. Example

for($i=1990;$i<2019,$i++){
 for($j=1;$j<5;$j++){
    $film = "API";
        $sve = file_get_contents($film);

        $data = json_decode($sve);

        foreach($data->results as $key => $value){
            $filmovi .= $value->title . ",";
        }
}

So, it must run five times in one year, like

1990 - 1,2,3,4,5

1991 - 1,2,3,4,5 etc etc..

Any suggestions?

标签: phploopsfor-loop

解决方案


The way you have it, will only run 4 times for each year and exclude 2019. Change your limits on the for loops:

for($i=1990;$i<=2019,$i++){
  $filmovi = "";
  for($j=1;$j<=5;$j++){
        $film = "API";
        $sve = file_get_contents($film);

        $data = json_decode($sve);

        foreach($data->results as $key => $value){
            $filmovi .= $value->title . ",";
        }
  }
}

推荐阅读