【问题标题】:Find index by value按值查找索引
【发布时间】:2012-12-01 07:07:47
【问题描述】:

大家晚上好。

首先,我为这个问题的长度道歉。我希望做到彻底,并确保所有信息都与我的意图一致。

我目前正在寻找一种简单的基于 Web 的方法来从我的一个桌面应用程序中收集统计信息。这样做的主要目的是进行故障排除并通过更新向最终用户提供更多相关的修复和功能添加。

详情

  • 我将使用 PHP 5.2.12,并利用 $_GET 从 URL 中获取参数并将它们分配给变量。
  • 这些变量随后将被json_encode 写入文件(称为“statsfile”或“stats 文件”)。
  • 其中一个参数/变量将是某种 GUID。这个 GUID 将由我的应用程序根据从硬件 ID 创建的单向哈希生成(尽管在代码示例中,这还没有表示出来)。

问题

我面临的问题与在我的 JSON 统计文件中查找特定 GUID 相关。

这就是我想做的;

  • 1) 在 statsfile 中找到提交的 GUID
  • 2a) 如果不存在,则向数组添加新索引并填充其中的数据
  • 2b) 如果确实存在,则用提交的数据覆盖数组中的现有数据

重要的是,最后我留下了一个有效的 JSON 统计文件。脚本相对安全也很重要。

布局和代码

我的JSON的布局如下;

[
    {
        "guid": "spritchard",
        "api": "apichoice",
        "build": "2200",
        "temp1": "1",
        "temp2": "2",
        "temp3": "3"
    },
    {
        "guid": "helloworld",
        "api": "someapi",
        "build": "3500",
        "temp1": "4",
        "temp2": "5",
        "temp3": "6"
    }
]

我使用的PHP代码如下;

<?php
$apikey=$_GET["apikey"];
if ($apikey=="apikey8634215")
  {
    $gpuapi=$_GET["api"];
    $buildno=$_GET["build"];
    $temp1=$_GET["temp1"];
    $temp2=$_GET["temp2"];
    $temp3=$_GET["temp3"];
    $guid=$_GET["guid"];

    $statsfile = "./api/application/StatsFile.json"; // Assign the stats file
    $fh = fopen($statsfile, 'a') // 'a' for append, 'w' for write
          or die("Stats File Unavailable");
    $sdata = array('guid' => $guid, 'api' => $gpuapi, 'build' => $buildno, 'temp1' => $temp1, 'temp2' => $temp2, 'temp3' => $temp3);
    fwrite($fh, json_encode($sdata));
    fclose($fh);
    echo json_encode($sdata);

    // reopen stats file to encode data as it should be
//    $cdata = fopen($statsfile, 'r') or die("Stats File Unavailable");
//    $encodedata = fread($cdata, filesize($statsfile));
//    $decodedata = json_decode($encodedata);
//    fclose($cdata);
//    $fh = fopen($statsfile, 'w') or die("Stats File Unavailable");
//    fwrite($fh, json_encode($encodedata));
//    fclose($fh);
  }
else
  {
    die("No Such Usage");
  }
?>

你可以看到我有$apikey 用于检查是否存在特定参数。如果不是,那么整个脚本就会死掉。目前这只是一个占位符设计,用于在我开发系统时从基本层面防止滥用,我将在单独的问题中研究更合适的解决方案。

注释掉的区域是我试图用新数据重写文件并使其成为有效 JSON 的代码。我不知道如何在数组中找到正确的 GUID,但我也在努力弄清楚如何重写数据以便将其正确编码为 JSON。取消注释该代码的最终结果是 JSON 中有很多斜杠,并且 JSON 本身无效。此外,我不相信我在这里展示的代码是安全的。无论如何,我可以使用POST 方法,但目前,找出如何根据它的 GUID 更新特定索引是个问题。

那么,为了澄清我的问题,我将如何从 JSON statsfile 中查找特定的 GUID、更新数据(如果存在)或使用提交的数据添加新的数组索引?

【问题讨论】:

  • 您为什么不使用为存储和检索 - 以及搜索 - 信息而设计的系统,例如数据库? sqlite 应该可用于大多数 PHP 安装。
  • 我没有对服务器的物理或根访问权限,因为它托管在共享虚拟主机上。此外,从软件方面来说,通过报告工具解析 JSON 文件比处理数据库要容易得多。

标签: php json getjson


【解决方案1】:
$apikey=$_GET["apikey"];
if ($apikey=="apikey8634215") {
    $newObject = new stdClass();
    $newObject->guid = $_GET["guid"];
    $newObject->api = $_GET["api"];
    $newObject->build = $_GET["build"];
    $newObject->temp1 = $_GET["temp1"];
    $newObject->temp2 = $_GET["temp2"];
    $newObject->temp3 = $_GET["temp3"];
    $statsFile = "./api/application/StatsFile.json";
    $stats = file_exists($statsFile)
        ? json_decode(file_get_contents($statsFile))
        : array()
    ;
    $found = false;
    foreach ($stats as $key => $statItem) {
        if ($statItem->guid == $newObject->guid) {
            $stats[$key] = $newObject;
            $found = true;
            break;
        }
    }
    if (!found) {
        $stats[] = $newObject;
    }
    if (file_put_contents($statsFile, json_encode($stats)) === false) {
        die("Could not save stats");
    }
} else {
    die("No Such Usage");
}

此代码缺少获取参数是否存在检查,您应该以某种方式验证输入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-09
    • 2016-02-09
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    • 2016-12-08
    • 2014-06-03
    相关资源
    最近更新 更多