2012-04-25 65 views
0

我試圖輸出數組的電子郵件值的值,但遇到問題。 陣列是基於json_decode()無法從數組中獲得值

這是錯誤我接收

Fatal error: Cannot use object of type stdClass as array in /home/.... line 57 

JSON(的值函數:$ this-> bck_content)

{"email":"[email protected]","membership_id":"0","fname":"Kenneth","lname":"Poulsen","userlevel":"1","created":"2012-04-23 10:57:45","lastlogin":"2012-04-23 10:58:52","active":"y"} 

我的代碼

# Display requested user details 
    $details_array = json_decode($this->bck_content); 

    $value = $details_array['email']; 
    print $value; 
+0

此代碼是否在類中執行? – 2012-04-25 18:00:27

回答

3

您需要使用json_decode的第二個參數來強制J上的數組結構S對象。

json_decode($this->bck_content, true); 

這將確保json中的所有JS對象都被解碼爲關聯數組而不是PHP StdObjects。

當然,這是假設你想使用數組符號來訪問它們。如果你細使用對象符號那麼你可以使用:

$value = $details_array->email; 
+0

非常感謝,一直試圖找到至少一個小時(可能接近2)的錯誤:p – 2012-04-25 18:06:10

1

試試這個

$value = $details_array->email; 

json_decode($json, true); 

$details_array = (array)json_decode($json); 

有什麼你做錯了寫在錯誤描述