【问题标题】:How to make number list appear x times? in PHP如何让号码列表出现x次?在 PHP 中
【发布时间】:2023-02-24 13:05:37
【问题描述】:

我有 $IDs 我希望每个 $IDs 出现 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
)

然后我从每个 $IDs 中得到 $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
)

然后我希望每个 $IDs 出现当前 $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..... 
)

【问题讨论】:

    标签: php arrays


    【解决方案1】:

    一个简单的 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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2016-12-02
        • 1970-01-01
        • 2019-09-25
        • 2021-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-01
        • 1970-01-01
        相关资源
        最近更新 更多