多维数组,返回错误的结果

时间:2013-01-12 06:42:44

标签: php arrays

我有一个多维数组(“数据”中有多个项目,但我只是为这个问题显示一个):

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [to] => Array
                        (
                            [data] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => fake name
                                            [id] => 668071477234
                                        )

                                    [1] => Array
                                        (
                                            [name] => fake name
                                            [id] => 1345556711
                                        )

                                )

                        )

                    [updated_time] => 2012-12-24T23:46:26+0000
                    [id] => 327424994013537
                )
        )

)

我试图循环通过数组并确定id是否匹配从$ _REQUEST发送的变量,如果匹配,我只想返回迭代的“updated_time”值。

这是我所拥有的,但日期总是错误的,并且与正确的迭代不匹配:

    foreach($userOutbox['data'] as $outbox){
        foreach($outbox['to']['data'] as $user){
            if($user['id'] == $_REQUEST['facebook_id']){                                           

                      $last_message_date = $outbox['updated_time'];
            }
        }
    }

已经很晚了,我的眼睛和大脑都没有帮助我。任何人都可以给我任何指示吗?

1 个答案:

答案 0 :(得分:0)

以下是适用于我的解决方案,只需添加break 2;感谢您的帮助:

foreach($userOutbox['data'] as $outbox){
    foreach($outbox['to']['data'] as $user){
        if($user['id'] == $_REQUEST['facebook_id']){                                           
           $last_message_date = $outbox['updated_time'];
           break 2;
        }
    }
}