从这个数组中动态获取数字的方法是什么?

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

How to Fetch Number from This Array Dynamically?

问题

我有Variable $IDs,它是TMDBIDs的列表。该Variable是一个数组,我已经使用foreach循环将其转换为数字列表,以便可以将其用作Variable $tmdbid,这一部分没有问题。

但是问题开始于我想要从tmdbapi数组中获取每个数据的Season_number时。如何做到这一点?可以有人帮助我吗?

这是我的代码。

现在我想要动态地从数组$seasons中获取每个TMDBID的Season_Number列表。

以下是来自数组$seasons的结果。

我想要获取每个[seasons] [season_number]的数据。

我已经尝试过以下方法,但没有得到我想要的结果。

如何使Seasons数组的索引更加动态,而不是固定为[0],以便能够列出所有的季节,而不仅仅是第一个季节。例如,如果电视节目有3个季节,它应该列出来所有3个季节。

谢谢。

英文:

Am working on my personal project.. But I have stuck here since yesterday without sucess.. Can Anyone help me please? And thanks in advance.

I have Variable $IDs which is list of TMDBIDs.. That Variable is array I have converted to number list using foreach loop so I can use as Variable $tmdbid, this one worked with no problem..

But The issue start here when I want to get Only Season_number of each data produced from tmdbapi array. How to do that.. Can Anyone help me Please?

Here is my code

NOTE: This is an Edit I have included all the code as requested by @RiggsFolly

<?php

$tmdbapi = "25ae6e919xxxxxxxxxxxxxx";

// Converting JSON to array
$movieslist = file_get_contents('https://example.com/dump/tv');
$tmdbdata = json_decode($movieslist, true);

//Filtering only TMDB ID

$IDs = array();
foreach($tmdbdata as $key => $val) {
    array_push($IDs, $tmdbdata[$key]);
}

$count = -1;

/*---------------------- The number of records to display per page -----------*/
$page_size = 10;

// Calculate total number of records, and total number of pages
$total_records = count($IDs);
$total_pages   = ceil($total_records / $page_size);

// Validation: Page to display can not be greater than the total number of pages
if ($page > $total_pages) {
    $page = $total_pages;
}

// Validation: Page to display can not be less than 1
if ($page < 1) {
    $page = 1;
}

// Calculate the position of the first record of the page to display
$offset = ($page - 1) * $page_size;

// Get the subset of records to be displayed from the array
$IDs = array_slice($IDs, $offset, $page_size);

// variables for pagination links
$page_first = $page > 1 ? 1 : '';
$page_prev  = $page > 1 ? $page-1 : '';
$page_next  = $page < $total_pages ? $page + 1 : '';
$page_last  = $page < $total_pages ? $total_pages : '';

/*--------------------------- Getting Seasons numbers from TMDBID ------------*/

$tvdata = array();
foreach ($IDs as $tmdbid1):
    $tvdata[] = file_get_contents('https://api.themoviedb.org/3/tv/'.$tmdbid1.'?api_key='.$tmdbapi.'&language=en-US');

    // Converting JSON to array
    $seasons = array();
endforeach;
$length = count($tvdata);
for ($i = 0; $i < $length; $i++) {
    $seasons[] = json_decode($tvdata[$i], true);
}

//Filtering only seasons_number

$seasons_numbers = array();
foreach ($seasons as $seasons_list) {
    $seasons_numbers[] = $seasons_list['seasons'][0]['season_number']; 
}

echo 'Printing Seasons';

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

?>

Now I want to Get list of every Season_Number from of each TMDBID dynamically from Array $seasons

Here is the Results from Array $seasons

Array
(
[0] => Array
(
[adult] => 
[backdrop_path] => /6ayRwd2P0K3d1TDaePcxvDkDjsv.jpg
[created_by] => Array()
[episode_run_time] => Array
(
[0] => 65
)
[first_air_date] => 2019-11-22
[genres] => Array
(
[0] => Array
(
[id] => 18
[name] => Drama
)
)
[homepage] => https://www.netflix.com/title/80244846
[id] => 86456
[in_production] => 
[languages] => Array
(
[0] => en
)
[last_air_date] => 2019-11-22
[last_episode_to_air] => Array
(
[air_date] => 2019-11-22
[episode_number] => 8
[id] => 1991644
[name] => These Old Bones
[overview] => A mysterious mountain woman steps into the spotlight as a lawyer struggles to find her voice in this story of seeing, believing and everything between.
[production_code] => 
[runtime] => 86
[season_number] => 1
[show_id] => 86456
[still_path] => /rpiimwUs6buZozBPF3kv08NohUX.jpg
[vote_average] => 5
[vote_count] => 3
)
[name] => Dolly Parton's Heartstrings
[next_episode_to_air] => 
[networks] => Array
(
[0] => Array
(
[id] => 213
[name] => Netflix
[logo_path] => /wwemzKWzjKYJFfCeiB57q3r4Bcm.png
[origin_country] => 
)
)
[number_of_episodes] => 8
[number_of_seasons] => 1
[origin_country] => Array
(
[0] => US
)
[original_language] => en
[original_name] => Dolly Parton's Heartstrings
[overview] => Eight stories celebrating family, faith, love and forgiveness come to life in this series inspired by Dolly Parton's iconic country music catalog.
[popularity] => 5.595
[poster_path] => /jW41XQ28sRYf7WDTtPuQaymlOxW.jpg
[production_companies] => Array
(
[0] => Array
(
[id] => 1957
[logo_path] => /3T19XSr6yqaLNK8uJWFImPgRax0.png
[name] => Warner Bros. Television
[origin_country] => US
)
[1] => Array
(
[id] => 5842
[logo_path] => 
[name] => Sandollar Productions
[origin_country] => US
)
[2] => Array
(
[id] => 114326
[logo_path] => 
[name] => Magnolia Hill Productions
[origin_country] => US
)
)
[production_countries] => Array
(
[0] => Array
(
[iso_3166_1] => US
[name] => United States of America
)
)
[seasons] => Array
(
[0] => Array
(
[air_date] => 2019-11-22
[episode_count] => 8
[id] => 118100
[name] => Season 1
[overview] => 
[poster_path] => /jW41XQ28sRYf7WDTtPuQaymlOxW.jpg
[season_number] => 1
)
)
[spoken_languages] => Array
(
[0] => Array
(
[english_name] => English
[iso_639_1] => en
[name] => English
)
)
[status] => Ended
[tagline] => 8 Dolly songs. 8 Dolly stories.
[type] => Miniseries
[vote_average] => 7.2
[vote_count] => 18
)
[1] => Array
(
[adult] => 
[backdrop_path] => 
[created_by] => Array
(
)
[episode_run_time] => Array
(
[0] => 25
)
[first_air_date] => 1994-10-06
[genres] => Array
(
[0] => Array
(
[id] => 10751
[name] => Family
)
[1] => Array
(
[id] => 35
[name] => Comedy
)
)
[homepage] => 
[id] => 20507
[in_production] => 
[languages] => Array
(
)
[last_air_date] => 1997-08-26
[last_episode_to_air] => Array
(
[air_date] => 1997-08-26
[episode_number] => 12
[id] => 586113
[name] => Season 3, Episode 12
[overview] => 
[production_code] => 
[runtime] => 25
[season_number] => 3
[show_id] => 20507
[still_path] => 
[vote_average] => 0
[vote_count] => 0
)
[name] => Pirates
[next_episode_to_air] => 
[networks] => Array
(
)
[number_of_episodes] => 18
[number_of_seasons] => 3
[origin_country] => Array
(
[0] => GB
)
[original_language] => en
[original_name] => Pirates
[overview] => Pirates is a British children's television sitcom about a family of pirates living in a council house. It featured a number of bizarre characters, such as the "Man in a Sack" and a baby in a pram which was never seen, but gave off a mysterious green glow. The series ran from 1994 to 1997 on Children's BBC, and featured Liz Smith as "Gran".
[popularity] => 0.856
[poster_path] => 
[production_companies] => Array ()
[production_countries] => Array ()
[seasons] => Array
(
[0] => Array
(
[air_date] => 1994-10-06
[episode_count] => 5
[id] => 30535
[name] => Season 1
[overview] => 
[poster_path] => 
[season_number] => 1
)
[1] => Array
(
[air_date] => 1995-10-04
[episode_count] => 5
[id] => 30536
[name] => Season 2
[overview] => 
[poster_path] => 
[season_number] => 2
)
[2] => Array
(
[air_date] => 1997-03-18
[episode_count] => 8
[id] => 30537
[name] => Season 3
[overview] => 
[poster_path] => 
[season_number] => 3
)
)
[spoken_languages] => Array
(
)
[status] => Ended
[tagline] => 
[type] => Scripted
[vote_average] => 0
[vote_count] => 0
)
And more Arrays.....

I want to get data of every [seasons] [season_number]

How can I Do it?

I have Tried

$seasons_numbers = array();
foreach ($seasons as $seasons_list) {
$seasons_numbers[] = $seasons_list['seasons'][0]['season_number']; 
}

The result

Array
Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 1
[5] => 1
[6] => 
[7] => 1
[8] => 1
[9] => 1
)

It means it only take The ID from the first seasons only.. and it not dynamic

Then I have tried

$seasons_numbers = array();
foreach ($seasons as $seasons_list) {
$seasons_numbers[] = $seasons[$i -1]['seasons'][0]['season_number']; 
}

And getting the same results..

How can I make it Index of Seasons Array more dynamic instead of putting [0] it should be something dynamic ['seasons'][0]['season_number'];

How to make it dynamic and list all seasons and not only the first season, for example if the tvshow have 3 seasons it should list all 3 seasons from the tvshow

Thanks.

答案1

得分: 1

以下是代码的翻译部分:

$page = 1;  // 用于测试的设置

$tmdbapi = "25ae6e919c5b9ee9058e04b9987bd921";

// 将 JSON 转换为数组
$movieslist = file_get_contents('https://listmovies.9animetv.live/dump/tv');
$tmdbdata = json_decode($movieslist, true);

// 仅筛选 TMDB ID

$IDs = array_values($tmdbdata);

$count = -1;

/*---------------------- 每页显示的记录数 -----------*/
$page_size = 10;

// 计算总记录数和总页数
$total_records = count($IDs);
$total_pages = ceil($total_records / $page_size);

// 验证:要显示的页面不能大于总页数
if ($page > $total_pages) {
    $page = $total_pages;
}

// 验证:要显示的页面不能小于 1
if ($page < 1) {
    $page = 1;
}

// 计算要显示的页面的第一条记录的位置
$offset = ($page - 1) * $page_size;

// 从数组中获取要显示的记录子集
$IDs = array_slice($IDs, $offset, $page_size);

// 分页链接的变量
$page_first = $page > 1 ? 1 : '';
$page_prev = $page > 1 ? $page - 1 : '';
$page_next = $page < $total_pages ? $page + 1 : '';
$page_last = $page < $total_pages ? $total_pages : '';

/*--------------------------- 从 TMDBID 获取季节数目 ------------*/

$tvdata = array();
foreach ($IDs as $id):
    if ($id != 0) :
        $tvdata[] = file_get_contents('https://api.themoviedb.org/3/tv/' . $id . '?api_key=' . $tmdbapi . '&amp;language=en-US');
    endif;
endforeach;

// 将 JSON 转换为数组
$seasons = [];
$length = count($tvdata);
foreach ($tvdata as $tvd) {
    $seasons[] = json_decode($tvd);     // 使用对象 :)
}

// 仅筛选季节数目

$seasons_numbers = [];
foreach ($seasons as $aSeason) {
    foreach ($aSeason->seasons as $season) {
        $seasons_numbers[] = $season->season_number; 
    }
}

echo '打印季节数目';

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

?>

结果:

Array
(
    [0] => 1
    [1] => 1
    [2] => 1
    [3] => 1
    [4] => 1
    [5] => 1
    [6] => 2
    [7] => 3
    [8] => 1
    [9] => 2
    [10] => 3
    [11] => 1
    [12] => 1
)
英文:

A little study of the shape of the data returned and I get here

$page = 1;  // set for testing
$tmdbapi = &quot;25ae6e919c5b9ee9058e04b9987bd921&quot;;
// Converting JSON to array
$movieslist = file_get_contents(&#39;https://listmovies.9animetv.live/dump/tv&#39;);
$tmdbdata = json_decode($movieslist, true);
//Filtering only TMDB ID
$IDs = array_values($tmdbdata);
$count = -1;
/*---------------------- The number of records to display per page -----------*/
$page_size = 10;
// Calculate total number of records, and total number of pages
$total_records = count($IDs);
$total_pages   = ceil($total_records / $page_size);
// Validation: Page to display can not be greater than the total number of pages
if ($page &gt; $total_pages) {
$page = $total_pages;
}
// Validation: Page to display can not be less than 1
if ($page &lt; 1) {
$page = 1;
}
// Calculate the position of the first record of the page to display
$offset = ($page - 1) * $page_size;
// Get the subset of records to be displayed from the array
$IDs = array_slice($IDs, $offset, $page_size);
// variables for pagination links
$page_first = $page &gt; 1 ? 1 : &#39;&#39;;
$page_prev  = $page &gt; 1 ? $page-1 : &#39;&#39;;
$page_next  = $page &lt; $total_pages ? $page + 1 : &#39;&#39;;
$page_last  = $page &lt; $total_pages ? $total_pages : &#39;&#39;;
/*--------------------------- Getting Seasons numbers from TMDBID ------------*/
$tvdata = array();
foreach ($IDs as $id):
if ( $id != 0 ) :
$tvdata[] = file_get_contents(&#39;https://api.themoviedb.org/3/tv/&#39;.$id.&#39;?api_key=&#39;.$tmdbapi.&#39;&amp;language=en-US&#39;);
endif;
endforeach;
// Converting JSON to array
$seasons = [];
$length = count($tvdata);
foreach ($tvdata as $tvd) {
$seasons[] = json_decode($tvd);     // use the object :)
}
//Filtering only seasons_number
$seasons_numbers = [];
foreach ($seasons as $aSeason) {
foreach ( $aSeason-&gt;seasons as $season) {
$seasons_numbers[] = $season-&gt;season_number; 
}
}
echo &#39;Printing Seasons&#39;;
print &quot;&lt;pre&gt;&quot;;
print_r($seasons_numbers);
print &quot;&lt;/pre&gt;&quot;;
?&gt;

RESULT

Array
(
[0] =&gt; 1
[1] =&gt; 1
[2] =&gt; 1
[3] =&gt; 1
[4] =&gt; 1
[5] =&gt; 1
[6] =&gt; 2
[7] =&gt; 3
[8] =&gt; 1
[9] =&gt; 2
[10] =&gt; 3
[11] =&gt; 1
[12] =&gt; 1

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

发表评论

匿名网友

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

确定