从数组中获取所有存储的ID

时间:2020-11-06 17:39:23

标签: php

**

我有一个数组,该数组存储所有集的ID,具体取决于电视节目和季节的数量。每个系列都有不同的季节数。 我需要一个想法,无论它有多少季节,都如何从$seasons[]['episodes']获取所有ID。 我希望它返回阵列中所有情节的ID。**

Array ( 
        [0] => Array ( 
                [name] => Season 1 
                [image_id] => 2119 
                [episodes] => Array ( 
                    [0] => 2122 
                    [1] => 2123 
                    [2] => 2124 
                    [3] => 2125 
                    [4] => 2126 
                ) 
                [year] => 2017 
                [description] => Atypical is a coming-of-age television series created by Robia Rashid for Netflix. It focuses on the life of 18-year-old Sam Gardner (Keir Gilchrist), who is on the autism spectrum. The first season was released on August 11, 2017, consisting of eight episodes. 
                [position] => 0 
            ) 
        [1] => Array ( 
                [name] => Season 2 
                [image_id] => 2120 
                [episodes] => Array ( 
                    [0] => 2127 
                    [1] => 2128 
                    [2] => 2129 
                    [3] => 2130 
                    [4] => 2131 
                ) 
                [year] => 2018 
                [description] => The ten-episode second season was released on September 7, 2018. In October 2018, the series was renewed for a third season of ten episodes. 
                [position] => 0 
            ) 
        )

1 个答案:

答案 0 :(得分:0)

您正在寻找array_column函数。

<?php
   $results = array_column($seasons, 'episodes'); // this will return 2d array of arrays with 'episodes'
   $final_results = call_user_func_array('array_merge', $results); // this will make 1d array of 2d array
   print_r($final_results);
?>