-2

私は PHP が初めてで、多次元配列を使用してイベントを処理しています。何らかの理由で Foreach 関数を使用すると、最後のサブ配列に対してエラーがスローされます。何が間違っているのかわかりません。

多次元配列は以下です。キーは、event_id、day、track、session の順です。

Array
(
[718] => Array
    (
        [1] => Array
            (
                [1] => Array
                    (
                        [1745] => Array
                            (
                                [session_id] => 1745
                                [event_start_date] => 2010-12-02 09:00
                                [session_position] => 1
                            )

                        [1554] => Array
                            (
                                [session_id] => 1554
                                [event_start_date] => 2010-12-02 09:00
                                [session_position] => 2
                            )

                        [1557] => Array
                            (
                                [session_id] => 1557
                                [event_start_date] => 2010-12-02 09:00
                                [session_position] => 3
                            )

                        [1678] => Array
                            (
                                [session_id] => 1678
                                [event_start_date] => 2010-12-02 09:00
                                [session_position] => 4
                            )

                    )

                [2] => Array
                    (
                        [1555] => Array
                            (
                                [session_id] => 1555
                                [event_start_date] => 2010-12-02 09:00
                                [session_position] => 1
                            )

                    )

                [3] => Array
                    (
                        [1649] => Array
                            (
                                [session_id] => 1649
                                [event_start_date] => 2010-12-02 09:00
                                [session_position] => 1
                            )

                        [1672] => Array
                            (
                                [session_id] => 1672
                                [event_start_date] => 2010-12-02 09:00
                                [session_position] => 2
                            )

                        [1651] => Array
                            (
                                [session_id] => 1651
                                [event_start_date] => 2010-12-02 09:00
                                [session_position] => 3
                            )

                    )

            )

        [2] => Array
            (
                [2] => Array
                    (
                        [1559] => Array
                            (
                                [session_id] => 1559
                                [event_start_date] => 2010-12-02 09:00
                                [session_position] => 1
                            )

                        [1669] => Array
                            (
                                [session_id] => 1669
                                [event_start_date] => 2010-12-02 09:00
                                [session_position] => 2
                            )

                        [1650] => Array
                            (
                                [session_id] => 1650
                                [event_start_date] => 2010-12-02 09:00
                                [session_position] => 3
                            )

                    )

                [4] => Array
                    (
                        [1652] => Array
                            (
                                [session_id] => 1652
                                [event_start_date] => 2010-12-02 09:00
                                [session_position] => 1
                            )

                        [1561] => Array
                            (
                                [session_id] => 1561
                                [event_start_date] => 2010-12-02 09:00
                                [session_position] => 2
                            )

                        [1654] => Array
                            (
                                [session_id] => 1654
                                [event_start_date] => 2010-12-02 09:00
                                [session_position] => 3
                            )

                    )

                [5] => Array
                    (
                        [1638] => Array
                            (
                                [session_id] => 1638
                                [event_start_date] => 2010-12-02 09:00
                                [session_position] => 1
                            )

                        [1556] => Array
                            (
                                [session_id] => 1556
                                [event_start_date] => 2010-12-02 09:00
                                [session_position] => 2
                            )

                        [1653] => Array
                            (
                                [session_id] => 1653
                                [event_start_date] => 2010-12-02 09:00
                                [session_position] => 3
                            )

                    )

            )

    )

)

次のコードを使用して配列を表示しています(バグが修正されたら処理します)

reset($rec_info); // the above array
foreach ($rec_info as $rec_event => $event_details) {
    echo "event is: " . $rec_event . "<br />";
    foreach ($event_details as $rec_day => $day_details){
        echo "day is: " . $rec_day . "<br />";
        foreach ($day_details as $rec_track => $track_details){
            echo "track is: " . $rec_track . "<br />"; 
            foreach ($track_details as $rec_session => $session_details) {
                echo "session_id is: " . $session_details["session_id"]. "<br />"; 
            }
        }
    }
} 

これは表示されます

event is: 718
day is: 1
track is: 1
session_id is: 1745
session_id is: 1554
session_id is: 1557
session_id is: 1678
track is: 2
session_id is: 1555
track is: 3
session_id is: 1649
session_id is: 1672
session_id is: 1651
day is: 2

Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/dev/global.php on line 349

他のイベント (2 ~ 5 日) を処理する場合、この警告がスローされるのは常に最終日です。

コードが他のすべての日では機能するのに、最後の日では機能しない理由がわかりません。配列が境界を超えたかのようです。その日を除いて、print_rを使用してすべてのサブアレイを正しく表示できます。

echo "<hr> tracks : <pre>"; print_r($track_details); echo "</pre> <br />";
4

1 に答える 1