2013-05-27 61 views
0

我想此腳本從最初找到一組變量如(playtime_forever)尋找另一個變量如(backpack_value)PHP解析來自一個JSON API頁

if (!empty($data->response->games)) { 
    foreach ($data->response->games as $game) { 
    if ($game->appid == $game_id) { 
     $playtime = $game->playtime_forever; 
     if (($playtime < 5940) && ($playtime > 1)) { 
     $fh = fopen("final.".$timestamp.".txt", 'a') or die("Can't open file"); 
... 

頁轉換中的一行解析看起來像這樣 http://pastebin.com/rnnCsijd

但現在是這樣。從這裏拉出 http://backpack.tf/api/IGetUsers/v2/?&steamids=76561197992146126&format=json%27;

{ 
    "response": { 
     "success": 1, 
     "current_time": 1369669066, 
     "players": { 
      "0": { 
       "steamid": "76561197992146126", 
       "success": 1, 
       "backpack_value": 36412.71, 
       "backpack_update": 1369630863, 
       "name": ":HIT: Bobo the Monkey Boy", 
       "notifications": 0 
      } 
     } 
    } 
} 

一個小小的變化,但我無法讓腳本做我想做的。如果你能解釋如何解決這個問題及其背後的步驟,那就太好了。我一直在嘗試了一段時間,但我無法完成了劇本

+2

這兩個JSON文件是什麼一樣。新的JSON沒有舊的JSON中的任何鍵。 什麼數據以什麼方式你甚至試圖從新的JSON中檢索? – Putr

+0

上面的完整代碼就在這裏。 http://justpaste.it/2pws。我希望這個新的腳本解析出「backpack_value」低於一定數量的配置文件。 – SuperMar1o

+0

請相應地更新您的問題,並使其更清晰可讀。 – Putr

回答

1

要獲取配置文件,按您的評論,請使用此代碼:

$profiles = array(); //init array so we can use $profiles[] later 
$limitValue = 1000; //Limit value of backpack_value 

foreach($data->response->players as $player) { // Loop thrugh all the players 
    if ($player->backpack_value < $limitValue) { // Check the backpack_value 
    $profiles[] = $player; // Assign the required players to a new array 
    } 
} 
var_dump($profiles); // Dump the array to browser for debugning 
+0

會快速嘗試這個,並與你回去。謝謝... – SuperMar1o

+0

我實現了它,它不輸出任何東西。有什麼建議麼? http://justpaste.it/2pyn(這是unique.txt的內容http://justpaste.it/2pyo)我會亂它,但也許你注意到我錯過的東西 – SuperMar1o

+0

我認爲你的問題在這裏'if(!empty($ data-> backpack_value)){'因爲'$ data-> backpack_value'不存在。你應該嘗試'if(!empty($ data))'或者'if($ data-> response-> success))' – MISJHA