Question

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?

Was it helpful?

Solution

update $test_data instead of $value

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

OTHER TIPS

You need to nest a furthermore.

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

Use like this,

foreach ($test_data as $key => $value) {
                      $test_data[$key]['test_duration'] = ConvertTimeStampToTimeFormate($value['test_duration']);
                    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top