PHP Foreach在最后一个子阵列上出错

时间:2013-05-28 16:52:03

标签: php

我是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 />";

1 个答案:

答案 0 :(得分:0)

来自PHP manual for foreach

  

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标记的正确文本编辑器应突出显示错误。