繁体   English   中英

多维数组的访问元素

[英]Accessing elements of multi-dimensional array

新手数组问题...

给定具有以下结构的数组:

$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
        }

}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM