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;
}

它确实有效,可以替换它,但我收到警告:

警告:不能将标量值用作数组

为什么?以及如何解决?

4

2 回答 2

1

看起来您的某些根数组元素不是数组,例如:

[new_messages] => 0

所以没有timestamp进入的钥匙。只需添加:

if(!is_array($row))
  continue;
于 2013-05-12T19:46:23.593 回答
0

它还想更改 key 的时间戳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;
}
于 2013-05-12T19:47:51.683 回答