[英]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.