2014-07-05 70 views
1

$ gameAPI->遊戲最大值

"games": [ 
     { 
      "appid": 10, 
      "playtime_forever": 32 
     }, 
     { 
      "appid": 20, 
      "playtime_forever": 0 
     }, 
     { 
      "appid": 30, 
      "playtime_forever": 0 
     }, 
     ] 

我想在那裏playtime_forever在列表中的最高值的appid + playtime_forever數據。

我用下面的代碼:

$data = array_reduce($data, function ($a, $b) { 
    return @$a['playtime_forever'] > $b['playtime_forever'] ? $a : $b; 
}); 

其中$數據= $ gameAPI->遊戲;

但是我得到了以下錯誤:不能使用類型爲stdClass的對象作爲數組。在線:

return @$a['playtime_forever'] > $b['playtime_forever'] ? $a : $b; 

回答

2

由於您的錯誤狀態,您正在使用對象作爲數組。要訪問PHP中的對象屬性,使用箭頭符號->

return $a->playtime_forever > $b->playtime_forever ? $a : $b; 
+0

謝謝你,這工作:)。 – Terry

+0

@Terry接受答案最好「謝謝你」 – Justinas