-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

0

来自foreach 的 PHP 手册

foreach仅适用于数组和对象,当您尝试在具有不同数据类型的变量或未初始化的变量上使用它时会发出错误。

在尝试使用 循环非数组之前,您需要检查它们是否是有效数组foreach()

<?php

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

检查数据库或初始数组是否有任何异常(意外或未关闭的记录)。很可能是线348349。如果错误是语法错误,带有 PHP 标记的适当文本编辑器应突出显示该错误。

于 2013-05-28T17:39:41.440 回答