在PHP中如何使数字列表出现x次

wgmfuz8q  于 2023-02-28  发布在  PHP
关注(0)|答案(3)|浏览(107)

我有$ID,我希望每个$ID出现x次number_of_seasons,怎么做?
下面是我的代码

    • 我有数组$ID**
$IDs = Array
(
    [0] => 86456
    [1] => 83981
    [2] => 3444
    [3] => 2296
    [4] => 21515
    [5] => 66707
    [6] => 20507
    [7] => 66985
    [8] => 78067
    [9] => 10364
)
    • 然后我有每个$ID的$number_of_seasons**
$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
)
    • 然后我希望每个$ID在当前$number_of_seasons中出现x次**
    • 预期结果如下:$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
)

"我试过了"

*----------------- 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>";
    • 结果出乎意料

下面是我从$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..... 
)
wribegjk

wribegjk1#

一个简单的foreach()和一个for()循环就可以完成这项工作:

$finalArray = [];

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

print_r($finalArray);

https://3v4l.org/fXvN2
或者使用array_fill()也可以完成这项工作:

$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

ruyhziif

ruyhziif2#

内部循环使它再次开始循环,你只需要一个循环。

foreach ($number_of_seasons as $idx => $number_of_seasons1){
    $id3 = array_merge($id3, array_fill (0, $number_of_seasons1, $IDs[$idx]));
}
a64a0gku

a64a0gku3#

像这样试试

$IDs = array(
    86456,
    83981,
    3444,
    2296,
    21515,
    66707,
    20507,
    66985,
    78067,
    10364
);

$number_of_seasons = array(
    1,
    1,
    1,
    1,
    1,
    3,
    3,
    1,
    1,
    1
);

foreach ($IDs as $key => $id) {
    $seasons = $number_of_seasons[$key];
    for ($i = 0; $i < $seasons; $i++) {
        echo $id . "\n";
    }
}

在这段代码中,我们首先使用foreach循环迭代**$IDs中的每个ID。对于每个ID,我们使用$key变量从$number_of_seasons中检索相应的季节数。然后使用嵌套的for循环根据$seasons的值打印IDx**次。

相关问题