Search code examples
phparraysassociative-arraydynamic-arraysarray-key

Why is the value of array key not getting updated?


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?


Solution

  • update $test_data instead of $value

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