英文:
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] => 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.....
)
答案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=>$id){
for($i=0; $i<$number_of_seasons[$key];$i++){
$finalArray[] = $id;
}
}
print_r($finalArray);
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);
答案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 => $number_of_seasons1){
$id3 = array_merge($id3, array_fill (0, $number_of_seasons1, $IDs[$idx]));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论