A novice array question…
Given an array with this structure:
$post_dates Array [1]
[0...0]
2640 Array [2]
[0...1]
_id 2640
date_posted MongoDate
How would I access the date_posted element when the 1st level key is not always the same?
I thought I could just have this:
$post_dates[0]['date_posted']
But that’s giving me an ‘Undefined Offset’ message.
I’ve also tried
$post_dates[0][1]
but that gives the same message.
This is the code block where I am getting the error:
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
}
}
If you don’t know any of the keys in the chain
$array["known"]["also_known"][???]["date_posted"], you’ll have to iterate the array in order to figure out which has thedate_postedyou need.