我正在嘗試從 Steam 服務獲取我的項目列表。我有兩個功能,其中1個:從Steam商店接收所有物品,2個函數接收物品的價格,名稱,圖片。因此,當我嘗試解析數據時,在我的一個帳戶中,我收到一個錯誤:,而在我的第二個帳戶中,所有解析都很好。Undefined property: stdClass::$Unblinking Eternityvar_dump($items); exit;給我看:object(stdClass)#547 (34183) { ["Orb of Deliverance"]=> object(stdClass)#546 (2) { ["price"]=> float(58.31) ["appid"]=> string(3) "570" } ["Planks of the Bogatyr"]=> object(stdClass)#54.......和一些日志:at HandleExceptions->handleError('8', 'Undefined property: stdClass::$Unblinking Eternity', '/var/app/Http/Controllers/InventoryController.php', '320', array('market_hash_name' => 'Unblinking Eternity', 'json' => '{"Orb of Deliverance":{"price":57.780000000000001,"appid":"570","count":427,"bprice":51.490000000000002,"bcount":11663},"Planks of the Bogatyr":{"price":0.14000000000000001,"appid":"570","count":922,"bprice":0.12,"bcount":859},"Headdress of the Ember Crane":{"price":0.16,"appid":"570","count":808,"bprice":0.14999999999999999,"bcount":218},"Sash of Divine Ascension":錯誤顯示在320行,320行它:if($items->$item_name == 'undefined'){我的2個功能:public static function getItemPrice($market_hash_name) { try { if(\Cache::has('prices')){ $json = \Cache::get('prices'); $items = json_decode($json); $item_name = $market_hash_name; if($items->$item_name == 'undefined'){ return false; } else{ return $items->$item_name->price; } } else { return false; } } catch(Exception $e){ return false; }}我如何理解,一個問題。stdClass我的錯誤在哪里?我該如何修復此錯誤?
1 回答

天涯盡頭無女友
TA貢獻1831條經驗 獲得超9個贊
在中,您引用了動態屬性名稱,而沒有首先檢查它是否存在。if ($items->$item_name == 'undefined') {
您可以使用 進行檢查。isset()
if (!isset($items->$item_name) || $items->$item_name == 'undefined') {
不過,我不知道該項目是否真的會是字符串“未定義”,所以你也許可以完全刪除該部分。
if (!isset($items->$item_name)) {
- 1 回答
- 0 關注
- 89 瀏覽
添加回答
舉報
0/150
提交
取消