我有一个名为$test_data
的数组,我想要更新密钥['test_duration']
。但是,我无法进行此更新。请考虑以下数组:
Array
(
[0] => Array
(
[test_id] => 1116
[test_name] => ques stats
[test_no_questions] => 50
[test_duration] => 28800
)
[1] => Array
(
[test_id] => 1112
[test_name] => Own Test 1
[test_no_questions] => 2
[test_duration] => 7200
)
)
我尝试了以下方法,但没有成功:
foreach ($test_data as $key => $value) {
$value[$key]['test_duration'] = ConvertTimeStampToTimeFormate($value['test_duration']);
}
如果我在此操作后打印数组,它将打印与以前相同的数组。这有什么问题?
答案 0 :(得分:3)
更新$ test_data而不是$ value
foreach ($test_data as $key => $value) {
$test_data[$key]['test_duration'] = ConvertTimeStampToTimeFormate($value['test_duration']);
}
答案 1 :(得分:2)
你需要进一步嵌套。
foreach ($test_data as $arr)
{
foreach($arr as $k=>$v)
{
$value[$k]['test_duration'] = ConvertTimeStampToTimeFormate($value['test_duration']);
}
}
答案 2 :(得分:0)
像这样使用,
foreach ($test_data as $key => $value) {
$test_data[$key]['test_duration'] = ConvertTimeStampToTimeFormate($value['test_duration']);
}