当我尝试从数组中获取时间戳索引时,我得到一个未定义的索引错误。
这是我的页面源
中的多维数组Array
(
[0] => Array
(
[timestamp] => 14:58:25
)
[1] => Array
(
[timestamp] => 14:58:43
)
[2] => Array
(
[timestamp] => 14:58:54
)
[3] => Array
(
[timestamp] => 15:06:28
)
)
Array
(
[0] => Array
(
[timestamp] => 08:53:26
)
[1] => Array
(
[timestamp] => 09:19:42
)
[2] => Array
(
[timestamp] => 11:41:51
)
[3] => Array
(
[timestamp] => 11:44:01
)
[4] => Array
(
[timestamp] => 12:09:23
)
)
这是我的功能代码:
private function getTimestamps ( $employee_id, $date )
{
$em = $this->getDoctrine()->getManager();
$sql = "SELECT TIME(time_record) as timestamp FROM log WHERE `employee_id` = '{$employee_id}' AND DATE(time_record) = '{$date}'";
$stmt = $em->getConnection()->prepare($sql);
$stmt->execute();
$data = $stmt->fetchAll();
$output = array ();
foreach ( $data as $d ) {
$output[] = $d;
}
return $output;
}
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$sql = "SELECT e.id as employee_id,
e.firstname as firstname,
e.lastname as lastname,
DATE(l.time_record) AS requested_date
FROM employee e
INNER JOIN log l
ON l.employee_id = e.id
GROUP BY DATE(l.time_record), l.employee_id";
$stmt = $em->getConnection()->prepare($sql);
$stmt->execute();
$logs = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$timetracking = array ();
foreach ( $logs as $log ) {
$timetracking[$log['employee_id']][] = $this->getTimestamps ( $log['employee_id'], $log['requested_date'] );
}
foreach ( $timetracking[11] as $time ) {
echo $time['timestamp'] . " ----- ";
//print_r($time);
}
die ();
}
我在这里做错了什么?请帮忙。
答案 0 :(得分:1)
我没有得到你的目标和观点但是尝试:
foreach ( $timetracking[11][0] as $time ) {
echo $time['timestamp'] . " ----- ";
//print_r($time);
}
根据你的评论你需要这个:
foreach ( $timetracking as $next ) {
echo '---NEXT---';
foreach ( $next[0] as $time ) {
echo $time['timestamp'] . " ----- ";
//print_r($time);
}
}
编辑3:
或者如果我终于实现了你的目标:
foreach ( $logs as $log ) {
$timetracking[$log['employee_id']] = $this->getTimestamps ( $log['employee_id'], $log['requested_date'] );
}
foreach ( $timetracking as $key=>$time ) {
echo 'empoyee_id : '.$key. '; time '.$time['timestamp'] . " \n";
}
编辑4 因此,您已发送评论(请勿再次发送,您应修改原始帖子并对问题添加修改):
<?php foreach ($logs as $log): ?>
<tr> <td><?php echo $log['firstname'] ?></td>
<td><?php echo date("d/m/Y") ?></td>
<?php foreach ( $timetracking[$log['employee_id']] as $next ): ?>
<?php foreach ($next[0] as $time): ?> <td>
<?php echo $time['timestamp'] ?></td>
<?php endforeach ?>
此代码与我建议的最后一个代码完全不同!!
所以我再次猜测你的目标是什么,但如果你按照我建议的方式准备你的阵列{strong>编辑3 ,你可以尝试:
logs