2014-01-30 88 views
-2

我有一個名爲$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

RTFM:http://www.php.net/manual/en/control-structures .foreach.php – CBroe

回答

3

更新$ TEST_DATA而不是$值

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

您需要再嵌套一個。

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

使用這樣,

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