【问题标题】:Fix PHP condition when decoding JSON修复解码 JSON 时的 PHP 条件
【发布时间】:2019-03-06 06:25:33
【问题描述】:

我正在使用 php 解码一个 json 文件。

foreach 语句中,我检查我的 cd_driver 是否在来自 json 文件内容的数组中。如果它在数组中,则脚本会按预期更新其月份。如果没有,则脚本将新驱动程序写入 json 文件。

一切正常,但在更新其月份并将相同的驱动程序添加到 json 文件后,它也在执行else 条件。为什么会这样?

$dados[] = array('cd_driver' => $cd_driver, 'Driver' => $RSx["ds_name"], 'month' => $cd_month, 'bsaldo' => $saldoToJson);
$dados2 = array('cd_driver' => $cd_driver, 'Driver' => $RSx["ds_name"], 'month' => $cd_month, 'bsaldo' => $saldoToJson);
$jsonString = file_get_contents('bsaldo.json');
$data = json_decode($jsonString, true);

    if($data == ""){
        $newString = json_encode($dados);
        file_put_contents('bsaldo.json', $newString);
    }else {
        foreach ($data as $key => $value) {

            $mot = $value['cd_driver'];
            $array = array();
            array_push($array, $mot);

            if(in_array($cd_driver, $array)){
                $data[$key]['month'] = $cd_month;
                $newString = json_encode($data);
                file_put_contents('bsaldo.json', $newString);
            }else {
                array_push($data, $dados2);
                $finalString = json_encode($data);
                file_put_contents('bsaldo.json', $finalString);
            }

        }
    }

json:

[
    {
        "cd_driver": "11831",
        "Driver": "ADENILSON RODRIGUES DE SOUZA",
        "month": "02",
        "bsaldo": -903
    },
    {
        "cd_driver": "11835",
        "Driver": "EDIVAN DE CASTRO VASSALO",
        "month": "01",
        "bsaldo": -7670
    },
    {
        "cd_driver": "11831",
        "Driver": "ADENILSON RODRIGUES DE SOUZA",
        "month": "02",
        "bsaldo": -903
    },
    {
        "cd_driver": "11831",
        "Driver": "ADENILSON RODRIGUES DE SOUZA",
        "month": "02",
        "bsaldo": -903
    }
]

我不得不调整一些东西来使用 php 5.1。

【问题讨论】:

  • $cd_driver 在您的代码中似乎未定义。请参阅$mot = $value['cd_driver']; 并在每次迭代时重置此$array = array();。所以[$mot] 与该数组相同。或$cd_driver == $mot
  • 你在每个循环中清空 $array
  • 基本上这就是你正在做的事情,if(undefined == $mot){ - :) - 而不是这个 if(in_array($cd_driver, $array)){ 当然会落入条件的}else{ 部分。
  • I had to adapt some things to work with php 5.1 PHP 5.x 差不多完成了,这提醒我我需要从5.6升级我的服务器
  • undefined 是什么意思?因为循环?它将驱动程序更新到文件中。

标签: php json


【解决方案1】:

您的问题是您在循环的每次迭代中都重置了数组。将其移出foreach,如下所示:

$dados[] = array('cd_driver' => $cd_driver, 'Driver' => $RSx["ds_name"], 'month' => $cd_month, 'bsaldo' => $saldoToJson);
$dados2 = array('cd_driver' => $cd_driver, 'Driver' => $RSx["ds_name"], 'month' => $cd_month, 'bsaldo' => $saldoToJson);
$jsonString = file_get_contents('bsaldo.json');
$data = json_decode($jsonString, true);

    if($data == ""){
        $newString = json_encode($dados);
        file_put_contents('bsaldo.json', $newString);
    }else {
        $array = array(); # here it won't be continually reset and can accumulate values as intended.
        foreach ($data as $key => $value) {

            $mot = $value['cd_driver'];

            array_push($array, $mot);

            if(in_array($cd_driver, $array)){
                $data[$key]['month'] = $cd_month;
                $newString = json_encode($data);
                file_put_contents('bsaldo.json', $newString);
            }else {
                array_push($data, $dados2);
                $finalString = json_encode($data);
                file_put_contents('bsaldo.json', $finalString);
            }

        }
    }

【讨论】:

  • 不错。我应该已经这样做了。它解决了部分问题。现在我需要找到一种方法将 else 移到循环之外,因为它根据找到的寄存器数量执行了多次。
  • 如果这解决了您的问题,我建议您给出正确答案并提出新问题。
  • 我认为这仍然是问题的一部分,但无论如何谢谢。
猜你喜欢
  • 2019-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-26
  • 2013-11-05
  • 2011-09-10
  • 2019-06-09
相关资源
最近更新 更多