[英]in a document of MongoDB, i have a key/value such that value is an association array (inserted using php); how can iterate
[英]How to get the last inserted array key in mongodb using php?
我有一个收藏
[_id] => 1
[user_id] => 2
[work_details] => Array
(
[0] => Array
(
[login_date] => 2015-12-22
[login_time] => 16:40:00
[log_out_time] =>
)
[1] => Array
(
[login_date] => 2015-12-23
[login_time] => 11:40:00
[log_out_time] =>
)
)
更新我使用以下查询
$collect->update(array("user_id"=>(int)$user_id,"work_details.0.login_date"=>'2015-12-22'),array('$push' => array("work_details.0.log_out_time" =>$logout_time)));
所以为了更新最后一条记录,我需要知道最后一个键值
我需要更新 work_details[1] 的 log_out_time。如何使用 php 在 mongodb 中获取最后插入的数组键
工作代码
<?php
$work_details = Array (
0 => Array
(
'login_date' => '2015-12-22',
'login_time' => '16:40:00',
'log_out_time' => '',
),
1 => Array
(
'login_date' => '2015-12-23',
'login_time' => '11:40:00',
'log_out_time' => '',
)
);
//get last item of an array
$last_item = end($work_details);
//set pointer to fist item
reset($work_details);
$last_item_key = null;
//compare value of last item with all values of an array, if found equal, return that key
foreach($work_details as $key => $value) {
if($value === $last_item) {
$last_item_key = $key;
break;
}
}
echo $last_item_key;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.