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

huangapple go评论64阅读模式
英文:

How to make number list appear x times? in PHP

问题

我有$IDs,我想让每个$IDs出现x次number_of_seasons,该如何做?

以下是我的代码

我有数组$IDs

$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    // 在这个ID中,我们有3个季节
    [6] => 3    // 同样在这个ID中,我们有3个季节
    [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       // 这个要出现3次,就像number_of_seasons一样
    [6] => 66707      2nd
    [7] => 66707      3rd
    [8] => 20507       // 这个也要出现3次,就像number_of_seasons一样
    [9] => 20507       2nd
    [10] => 20507     3rd
    [11] => 66985
    [12] => 78067
    [13] => 10364
)

我尝试过...

*----------------- 列出TMDBID的倍数季节数 ---------------------------*/
// 获取季节数,以便我们可以列出tmdb id x次季节数

$number_of_seasons = [];
foreach ($seasons as $seasons_no) {
  $number_of_seasons[] = $seasons_no->number_of_seasons; // 成功获取季节数
}

// 列出tmdb id x次季节数

$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 '打印将用于剧集的id3';

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

结果出乎意料

这是我从$id3得到的意外结果

$id3 = Array
(
    [0] => 86456     // 它没有按照我的要求列出这些数字,然后再列出
    [1] => 83981
    [2] => 3444
    [3] => 2296
    [4] => 21515
    [5] => 66707
    [6] => 20507
    [7] => 66985
    [8] => 78067
    [9] => 10364    // 这里是ID的结尾
    [10] => 86456  // 然后它从头开始循环
    [11] => 83981
    [12] => 3444
    [13] => 2296
    [14] => 21515
    [15] => 66707
    [16] => 20507
    [17] => 66985
    [18] => 78067
    [19] => 10364    // 这里是结尾
    [20] => 86456    // 然后它开始一遍又一遍地循环,这不是我期望的......
)
英文:

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] =&gt; 86456
    [1] =&gt; 83981
    [2] =&gt; 3444
    [3] =&gt; 2296
    [4] =&gt; 21515
    [5] =&gt; 66707
    [6] =&gt; 20507
    [7] =&gt; 66985
    [8] =&gt; 78067
    [9] =&gt; 10364
)

Then I have $number_of_seasons from each $IDs

$number_of_seasons = Array
(
    [0] =&gt; 1
    [1] =&gt; 1
    [2] =&gt; 1
    [3] =&gt; 1
    [4] =&gt; 1
    [5] =&gt; 3    // In this ID we have 3 Seasons
    [6] =&gt; 3    // Same to this we have 3 seasons
    [7] =&gt; 1
    [8] =&gt; 1
    [9] =&gt; 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] =&gt; 86456
    [1] =&gt; 83981
    [2] =&gt; 3444
    [3] =&gt; 2296
    [4] =&gt; 21515
    [5] =&gt; 66707       // This one to appear 3 times as the number_of_seasons
    [6] =&gt; 66707      2nd
    [7] =&gt; 66707      3rd
    [8] =&gt; 20507       // This one too to appear 3 times as the number_of_seasons
    [9] =&gt; 20507       2nd
    [10] =&gt; 20507     3rd
    [11] =&gt; 66985
    [12] =&gt; 78067
    [13] =&gt; 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-&gt;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 &#39;Printing id3 that will be used for episodes&#39;;

print &quot;&lt;pre&gt;&quot;;
print_r($id3);
print &quot;&lt;/pre&gt;&quot;;

**The result come Unexpected

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

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

答案1

得分: 2

一个简单的`foreach()`结合`for()`循环即可完成任务:

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

或者使用`array_fill()`也可以完成任务:

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

    print_r($finalArray);
英文:

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

$finalArray = [];

foreach($IDs as $key=&gt;$id){
    for($i=0; $i&lt;$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=&gt;$id){
   $finalArray = array_merge($finalArray, array_fill (0, $number_of_seasons[$key], $id));
}

print_r($finalArray);

https://3v4l.org/LDRS9

答案2

得分: 2

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

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

The inner loop makes it to start looping again, you just need only one loop.

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

huangapple
  • 本文由 发表于 2023年2月24日 12:56:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75552762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定