Php 多维数组元素的存取
一个新手数组问题 给定具有此结构的数组:Php 多维数组元素的存取,php,arrays,multidimensional-array,Php,Arrays,Multidimensional 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 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
}
}
foreach($posts as$post){
$post_dates=iterator_to_array($posts_coll->find(array(“topic_id”=>$post[''id']),array(“date_post”=>true));
如果(空($post_dates)){//没有回复,则最后一次发布日期=发布日期
//$repries=0;
$lastPost=日期(“d-M-Y h:i:s”,$post['date_posted']->sec);
echo“条件1,最后发布日期:.$lastPost.”
;
}
elseif(count($post_dates)==1){//一个回复,因此最后一次发布日期=$post_dates[发布日期]
//$lastPost=日期(“d-M-Y h:i:s,$post_日期[0][1]->秒);
echo“条件2,最后发布日期:.$lastPost.”
;
var_dump($post_dates[0][''id']);
}
否则{
//如果有多个回复,则确定最长发布日期的代码
}
}
如果你不知道链中的任何键$array[“known”[“allown”][???][“date\u posted”]
,你必须迭代数组,以找出哪个键有你需要的date\u posted
。但我知道,posted date\u将始终处于相同的位置,所以我认为如果我使用索引,即[0][1],它将返回从发布日期开始的值。这就是为什么我不能理解未定义的偏移量消息。我在我的帖子中添加了更多的代码,以将其放到上下文中。即使它的深度相同,您仍然需要知道确切的密钥才能访问它。您需要迭代。