0

一个新手数组问题...

给定一个具有这种结构的数组:

$post_dates Array [1]   
    [0...0] 
        2640    Array [2]   
            [0...1] 
                _id 2640    
                date_posted MongoDate

当第一级键并不总是相同时,我将如何访问 date_posted 元素?我以为我可以有这个:

$post_dates[0]['date_posted']

但这给了我一个“未定义的偏移量”消息。我也试过

$post_dates[0][1]

但这给出了相同的信息。

这是我收到错误的代码块:

foreach($posts as $post){

        $post_dates = iterator_to_array($posts_coll->find(array("topic_id"=>$post['_id']), array("date_posted"=>true)));

        if (empty($post_dates)){ // No replies, therefore last post date = date_posted
            //$replies = 0;
            $lastPost = date("d-M-Y h:i:s", $post['date_posted']->sec);
            echo "condition 1, last post date: " . $lastPost . "<br>";
        }
        elseif (count($post_dates) == 1) { // One reply, therefore last post date = $post_dates[date_posted]            
            //$lastPost = date("d-M-Y h:i:s", $post_dates[0][1]->sec);
            echo "condition 2, last post date: " . $lastPost . "<br>";
            var_dump($post_dates[0]['_id']);
        }
        else {
            // code to determine max date_posted if there is more than one reply
        }

}
4

1 回答 1

0

如果您不知道链中的任何键,则$array["known"]["also_known"][???]["date_posted"]必须迭代数组以找出date_posted您需要的键。

于 2012-08-02T21:04:55.770 回答