doter1995 2013-05-27 15:44
浏览 55
已采纳

PHP从Json API页面中解析一行

I am trying to convert this script over from finding one set of variables eg(playtime_forever) to finding another variable eg(backpack_value)

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");
...

The page it originally parsed looked like this http://pastebin.com/rnnCsijd

but will now be this. Pulled from here 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
            }
        }
    }
}

A small change but I am unable to make the script do what I want. If you could explain how to go about this and the steps behind it, it would be great. I have been trying for a while but I am unable to finish the script

  • 写回答

1条回答 默认 最新

  • douan8473 2013-05-27 17:57
    关注

    To get the profiles as per your comment, use this code:

    $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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存