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 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测