-2

I have an array called $test_data, and I want to update a key ['test_duration']. However, I am unable to do this update. Consider the following array:

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
        )

)

I tried the following, but it didn't work out:

foreach ($test_data as $key => $value) {
    $value[$key]['test_duration'] = ConvertTimeStampToTimeFormate($value['test_duration']);
}

If I print the array after this manipulation, it's printing the same array as before. What is the problem here?

4

3 回答 3

3

更新 $test_data 而不是 $value

foreach ($test_data as $key => $value) {
    $test_data[$key]['test_duration'] = ConvertTimeStampToTimeFormate($value['test_duration']);
}
于 2014-01-30T12:58:40.107 回答
2

您还需要嵌套一个。

foreach ($test_data as $arr)
{
  foreach($arr as $k=>$v)
    {
     $value[$k]['test_duration'] = ConvertTimeStampToTimeFormate($value['test_duration']);
    }
}
于 2014-01-30T12:58:22.930 回答
0

像这样使用,

foreach ($test_data as $key => $value) {
                      $test_data[$key]['test_duration'] = ConvertTimeStampToTimeFormate($value['test_duration']);
                    }
于 2014-01-30T13:00:09.393 回答