Цикл по трем независимым массивам объектов класса std на основе значений ключей в PHP - PullRequest
0 голосов
/ 12 сентября 2018

У меня есть три stdClass массива объектов, которые хранятся в отдельных переменных. Я хочу просмотреть их, так как для каждой итерации массива 1 я хочу, чтобы все индексы из массива 2 - где Season_id в массиве 2 будут равны Season_id в массиве 1.

Затем для каждой итерации массива 2 я хочу, чтобы все индексы из массива 3 - где Episode_id в массиве 3 будет равен Episode_id в массиве 2.

Это мои массивы

    Array
        (
            [0] => stdClass Object
                (
                    [Season_id] => 1
                    [Season_number] => 1
                    [TV_Show_id] => 1
                )

            [1] => stdClass Object
                (
                    [Season_id] => 2
                    [Season_number] => 2
                    [TV_Show_id] => 1
                )

            [2] => stdClass Object
                (
                    [Season_id] => 3
                    [Season_number] => 3
                    [TV_Show_id] => 1
                )
        )

------------------- 2-й массив ---------------------

          Array
            (
                [0] => Array
                (
                    [0] => stdClass Object
                    (
                        [Episode_id] => 1
                        [Episode_title] => Pigeon Post
                        [Episode_number] => 2
                        [Season_id] => 1
                    )

                    [1] => stdClass Object
                    (
                        [Episode_id] => 2
                        [Episode_title] => The British ave Come (The Fallen Madonna)
                        [Episode_number] => 1
                        [Season_id] => 1
                    )

                )

                [1] => Array
                (
                    [0] => stdClass Object
                        (
                            [Episode_id] => 3
                            [Episode_title] => The Gateau From The Chateau
                            [Episode_number] => 7
                            [Season_id] => 2
                        )

                    [1] => stdClass Object
                        (
                            [Episode_id] => 4
                            [Episode_title] => Herr Flicks Revenge
                            [Episode_number] => 6
                            [Season_id] => 2
                        )

                    [2] => stdClass Object
                        (
                            [Episode_id] => 5
                            [Episode_title] => The Duel
                            [Episode_number] => 5
                            [Season_id] => 2
                        )

                    [3] => stdClass Object
                        (
                            [Episode_id] => 6
                            [Episode_title] => Swiftly and With Style
                            [Episode_number] => 4
                            [Season_id] => 2
                        )
                )

                [2] => Array
                (
                [0] => stdClass Object
                    (
                        [Episode_id] => 10
                        [Episode_title] => The Great Un-Escape
                        [Episode_number] => 6
                        [Season_id] => 3
                    )

                [1] => stdClass Object
                    (
                        [Episode_id] => 11
                        [Episode_title] => Pretty Maids All In A Row
                        [Episode_number] => 5
                        [Season_id] => 3
                    )

                [2] => stdClass Object
                    (
                        [Episode_id] => 12
                        [Episode_title] => Flight of Fancy
                        [Episode_number] => 4
                        [Season_id] => 3
                    )

                [3] => stdClass Object
                    (
                        [Episode_id] => 13
                        [Episode_title] => The Sausage In the Wardrobe
                        [Episode_number] => 3
                        [Season_id] => 3
                    )
                )
            )

------------------------- 3-й массив -------------------- ----------

        Array
            (
                [0] => Array
                (
                    [0] => stdClass Object
                        (
                            [Subtitle_id] => 1
                            [Subtitle_name] => title name
                            [Subtitle_download_link] => donwload-link.
                            [Subtitle_language] => english 
                            [Subtitle_version] => FQM
                            [hearing_impairment] => 
                            [Episode_id] => 1
                        )

                    [1] => stdClass Object
                        (
                            [Subtitle_id] => 2
                            [Subtitle_name] => title name
                            [Subtitle_download_link] => donwload-link.
                            [Subtitle_language] => english 
                            [Subtitle_version] => FQM
                            [hearing_impairment] => 
                            [Episode_id] => 1
                        )

                    [2] => stdClass Object
                        (
                            [Subtitle_id] => 3
                            [Subtitle_name] => title name
                            [Subtitle_download_link] => donwload-link.
                            [Subtitle_language] => english 
                            [Subtitle_version] => FQM
                            [hearing_impairment] => 
                            [Episode_id] => 1
                        )

                    [3] => stdClass Object
                        (
                            [Subtitle_id] => 4
                            [Subtitle_name] => title name
                            [Subtitle_download_link] => donwload-link.
                            [Subtitle_language] => english 
                            [Subtitle_version] => FQM
                            [hearing_impairment] => 
                            [Episode_id] => 1
                        )
                )

                [1] => Array
                    (
                    [0] => stdClass Object
                        (
                            [Subtitle_id] => 5
                            [Subtitle_name] => title name
                            [Subtitle_download_link] => donwload-link.
                            [Subtitle_language] => english 
                            [Subtitle_version] => FQM
                            [hearing_impairment] => 
                            [Episode_id] => 2
                        )

                    [1] => stdClass Object
                        (
                            [Subtitle_id] => 6
                            [Subtitle_name] => title name
                            [Subtitle_download_link] => donwload-link.
                            [Subtitle_language] => english 
                            [Subtitle_version] => FQM
                            [hearing_impairment] => 
                            [Episode_id] => 2
                        )

                    [2] => stdClass Object
                        (
                            [Subtitle_id] => 7
                            [Subtitle_name] => title name
                            [Subtitle_download_link] => donwload-link.
                            [Subtitle_language] => english 
                            [Subtitle_version] => FQM
                            [hearing_impairment] => 
                            [Episode_id] => 2
                        )

                    [3] => stdClass Object
                        (
                            [Subtitle_id] => 8
                            [Subtitle_name] => title name
                            [Subtitle_download_link] => donwload-link.
                            [Subtitle_language] => english 
                            [Subtitle_version] => FQM
                            [hearing_impairment] => 
                            [Episode_id] => 2
                        )

                    [4] => stdClass Object
                        (
                            [Subtitle_id] => 9
                            [Subtitle_name] => title name
                            [Subtitle_download_link] => donwload-link.
                            [Subtitle_language] => english 
                            [Subtitle_version] => FQM
                            [hearing_impairment] => 
                            [Episode_id] => 2
                        )

                    [5] => stdClass Object
                        (
                            [Subtitle_id] => 10
                            [Subtitle_name] => title name
                            [Subtitle_download_link] => donwload-link.
                            [Subtitle_language] => english 
                            [Subtitle_version] => FQM
                            [hearing_impairment] => 
                            [Episode_id] => 2
                        )

                    )

                [2] => Array
                (
                    [0] => stdClass Object
                        (
                            [Subtitle_id] => 11
                            [Subtitle_name] => title name
                            [Subtitle_download_link] => donwload-link.
                            [Subtitle_language] => english 
                            [Subtitle_version] => FQM
                            [hearing_impairment] => 
                            [Episode_id] => 3
                        )

                )
            )

Я попробовал это. Он отлично работает только для первой итерации массива 1. После этого третий цикл не дает ожидаемых результатов.

        for($i=0;$i<sizeof($seasons);$i++)
        {
            echo $seasons[$i]->Season_id." season id";
            echo "<br>";

            for($j=0;$j<sizeof($episodes);$j++)
            {
                if($episodes[$i][$j]->Season_id == $seasons[$i]->Season_id)
                {
                    echo $episodes[$i][$j]->Episode_number." episode id";
                    echo "<br>";

                    for($k=0;$k<sizeof($subtitles);$k++)
                    {
                        if($subtitles[$j][$k]->Episode_id == $episodes[$i][$j]->Episode_id)
                        {
                            echo $subtitles[$j][$k]->Subtitle_id." subtitles id";
                            echo "<br>";
                        }
                    }
                }
                    echo "<br>";
            }
            echo "<br>";
        }

Мне нужен такой результат:

   Season 1
        Episode 1
            Sub Title 1
            Sub Title 2
            Sub Title 3
        Episode 2
            Sub Title 1
            Sub Title 2
            Sub Title 3
            Sub Title 4

    Season 2
        Episode 1
            Sub Title 1
            Sub Title 2
            Sub Title 3
        Episode 2
            Sub Title 1
            Sub Title 2

1 Ответ

0 голосов
/ 12 сентября 2018

одна проблема, с которой вы здесь сталкиваетесь, - как структурировать данные, а вторая - как избежать N+1 select query проблемы

.

Поскольку подход ORM широко используется для обсуждения, я бы предложил создать Seasonepisodessub_model и обработать все функциональные возможности этой модели.

следующее должно показать вам, как это сделать

class Seasonepisodessub_model
{

    private $arrSeasons = [];
    private $arrEpisodes = [];

    private function addEpisodes()
    {
        $this->db->where_in('Season_id',array_keys($this->arrSeasons)); 
        $query = $this->db->get('Show_Episodes'); 

        foreach($query->result() AS $objEpisode)
        {
            if(isset($this->arrSeasons[$objEpisode->Season_id]))
            {
                if (!isset($this->arrSeasons[$objEpisode->Season_id]->arrEpisodes)) $this->arrSeasons[$objEpisode->Season_id]->arrEpisodes = [];
                $this->arrSeasons[$objEpisode->Season_id]->arrEpisodes[] = $objEpisode;
                $this->arrEpisodes[$objEpisode->Episode_id] = $objEpisode;
            }
        }
    }

    private function addSubs()
    {
        if (empty($this->arrEpisodes))  $this->addEpisodes();

        $this->db->where_in('Episode_id',array_keys($this->arrEpisodes)); 
        $this->db->from('Show_subtitles'); 
        $query=$this->db->get(); 

        foreach($query->result() AS $objSub)
        {
            if(isset($this->arrEpisodes[$objSub->Episode_id]))
            {
                if (!isset($this->arrEpisodes[$objSub->Episode_id]->arrSubs))   $this->arrEpisodes[$objSub->Episode_id]->arrSubs = [];
                $this->arrEpisodes[$objSub->Episode_id]->arrSubs[] = $objSub;
            }
        }
    }


    public function getSeasons($tvId)
    {
        //reset arrays
        $this->arrSeasons = [];
        $this->arrEpisodes = [];

        $this->db->where('TV_id',$tvId); 
        $query = $this->db->get('Show_Seasons'); 

        foreach($query->result() AS $objSeason)
        {
            $this->arrSeasons[$objSeason->Season_id] = $objSeason;
        }

        $this->addSubs();

        return array_values($this->arrSeasons);
    }
}

если вы позвоните getSeasons с одного из ваших контроллеров, вы увидите, насколько структурированы эти данные.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...