2016-06-07 42 views
0

我有多個鍵的對象在PHP的數組:PHP不能使用類型stdClass的的對象作爲陣列錯誤

Array 
(
[0] => stdClass Object 
    (
     [ID] => 4983 
     [post_id] => 56357 
     [date] => 2016-06-04 23:45:28   
    ) 

[1] => stdClass Object 
    (
     [ID] => 4982 
     [post_id] => 56241 
     [date] => 2016-06-04 22:58:27   
    ) 
) 

然後我使用以下改變日期格式:

foreach($results as &$row) {  
    $row['date'] = ago($row['date']); 
} 

但是我一直在收到Cannot use object of type stdClass as array錯誤。

對此爲何發生的任何建議?

+0

您需要使用'mysql_fetch_ *'函數獲取數據。你的'$ result'變量是一個'resource'(http://php.net/manual/en/function.mysql-query.php) –

+0

你根本不應該得到那個結果。你是json_encoding一個mysql結果句柄,它不是**結果本身。 –

+0

我刪除了問題的頂部。這是一個簡化版本。只要說我得到的結果,我需要替換'日期'。 –

回答

1

您需要將其作爲對象進行訪問。它的一系列對象......你的var_dump表明。這裏是你的for循環應該是什麼樣子......

foreach($results as &$row) {  
    $row->date = ago($row->date); 
} 

使用->是你如何訪問對象屬性。

+0

謝謝。得到它的工作。 –

相關問題