skip to Main Content

I have $IDs I want each $IDs to appear x times number_of_seasons how to do it?

Here is my code

I have array $IDs

$IDs = Array
(
    [0] => 86456
    [1] => 83981
    [2] => 3444
    [3] => 2296
    [4] => 21515
    [5] => 66707
    [6] => 20507
    [7] => 66985
    [8] => 78067
    [9] => 10364
)

Then I have $number_of_seasons from each $IDs

$number_of_seasons = Array
(
    [0] => 1
    [1] => 1
    [2] => 1
    [3] => 1
    [4] => 1
    [5] => 3    // In this ID we have 3 Seasons
    [6] => 3    // Same to this we have 3 seasons
    [7] => 1
    [8] => 1
    [9] => 1
)

Then I want each $IDs to appear x times of the current $number_of_seasons

Expecting The result to be like this: $id3

$id3 = Array
(
    [0] => 86456
    [1] => 83981
    [2] => 3444
    [3] => 2296
    [4] => 21515
    [5] => 66707       // This one to appear 3 times as the number_of_seasons
    [6] => 66707      2nd
    [7] => 66707      3rd
    [8] => 20507       // This one too to appear 3 times as the number_of_seasons
    [9] => 20507       2nd
    [10] => 20507     3rd
    [11] => 66985
    [12] => 78067
    [13] => 10364
)

I have tried…

*----------------- List TMDBID times number of seasons ---------------------------*/
// Getting Number of Seasons so that we can list tmdb id x times number of the season

$number_of_seasons = [];
foreach ($seasons as $seasons_no) {
  $number_of_seasons[] = $seasons_no->number_of_seasons; // Succesful getting number of seasons
}

// Listing tmdb id x times number of season

$id3 = array ();
foreach ($number_of_seasons as $number_of_seasons1){
  foreach ($IDs as $id2) {
    $id3 = array_merge($id3, array_fill (0, $number_of_seasons1, $id2));
  }
}


echo 'Printing id3 that will be used for episodes';

print "<pre>";
print_r($id3);
print "</pre>";

**The result come Unexpected

Here is the unexpected results that I get from $id3**

$id3 = Array
(
    [0] => 86456     // It not doing what I want it list this numbers then it list again
    [1] => 83981
    [2] => 3444
    [3] => 2296
    [4] => 21515
    [5] => 66707
    [6] => 20507
    [7] => 66985
    [8] => 78067
    [9] => 10364    // Here is the end of IDs
    [10] => 86456  // Then it start looping again from the start 
    [11] => 83981
    [12] => 3444
    [13] => 2296
    [14] => 21515
    [15] => 66707
    [16] => 20507
    [17] => 66985
    [18] => 78067
    [19] => 10364    // Here is the end
    [20] => 86456    // Then it start looping again..
    [21] => 83981
    [22] => 3444
    [23] => 2296
    [24] => 21515
    [25] => 66707
    [26] => 20507
    [27] => 66985
    [28] => 78067
    [29] => 10364    // here is the end
     // Then it start looping again and again and again which I ddn't expect..... 
)

2

Answers


  1. A simple foreach() with a for() loop will do the job:

    $finalArray = [];
    
    foreach($IDs as $key=>$id){
        for($i=0; $i<$number_of_seasons[$key];$i++){
            $finalArray[] = $id;
        }
    }
    
    print_r($finalArray);
    

    https://3v4l.org/fXvN2

    or use of array_fill() will do the job as well:

    $finalArray = [];
    foreach($IDs as $key=>$id){
       $finalArray = array_merge($finalArray, array_fill (0, $number_of_seasons[$key], $id));
    }
    
    print_r($finalArray);
    

    https://3v4l.org/LDRS9

    Login or Signup to reply.
  2. The inner loop makes it to start looping again, you just need only one loop.

    foreach ($number_of_seasons as $idx => $number_of_seasons1){
        $id3 = array_merge($id3, array_fill (0, $number_of_seasons1, $IDs[$idx]));
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search