2013-05-12 114 views
0

我有一個多維數組,我想取代它的timestamp場..更換多維數組值

Array 
(
    [new_messages] => 0 
    [0] => Array 
     (
      [id] => 42 
      [sender] => 4 
      [receiver] => 4 
      [message] => Test 
      [timestamp] => 1368178683 
      [read] => 1 
      [s_deleted] => 0 
      [r_deleted] => 0 
     ) 

    [1] => Array 
     (
      [id] => 44 
      [sender] => 4 
      [receiver] => 4 
      [message] => test2 
      [timestamp] => 1368181485 
      [read] => 1 
      [s_deleted] => 0 
      [r_deleted] => 0 
     ) 
) 

我運行:

foreach ($messageArray as $key => $row) { 
    $orderByDate[$key] = $row['timestamp']; 
    $newTimestamp = date("d-M-Y H:i:s", $row['timestamp']); 
    $messageArray[$key]['timestamp'] = $newTimestamp; 
} 

它的工作,取代它,但我得到一個警告:

警告:不能使用標量值作爲數組

爲什麼?以及如何解決它?

回答

1

它看起來像你的一些根數組元素不是數組,如:

[new_messages] => 0 

所以沒有timestamp鍵進入。只需添加:

if(!is_array($row)) 
    continue; 
0

也想改變的關鍵new_messages的時間戳,但沒有一個(它只是一個值)。

更改這個

foreach ($messageArray as $key => $row) { 
    if (!is_array($row) || !array_key_exists('timestamp', $row)) { 
    continue; 
    } 
    $orderByDate[$key] = $row['timestamp']; 
    $newTimestamp = date("d-M-Y H:i:s", $row['timestamp']); 
    $messageArray[$key]['timestamp'] = $newTimestamp; 
}