【问题标题】:How to update a decoded JSON array with new array in PHP?如何用 PHP 中的新数组更新解码的 JSON 数组?
【发布时间】:2018-09-21 17:11:24
【问题描述】:

我需要知道如何用新值更新解码后的 JSON 数组(不过我不是指简单的array_push)。

我要发布我的代码,这是我的Users.json 文件:

[
{
  "objID":"Y0FVVFZYCV",

  "createdOn":{"type":"__date","date":"2018-09-21T16:48:09"},
  "string":"lorem ipsum"
},
{
  "objID":"YShAUqIcMg",
  "username":"johndoe", // new key->value here!
  "createdOn":{"type":"__date","date":"2018-09-21T16:48:14"},
  "string":"lorem ipsum"
}
]

这是我的 create.php 文件,我曾在其中从 URL 字符串添加 JSON 对象:

// Prepare data to be saved:
if(!empty($_GET)){
    foreach($_GET as $key => $value) {

        // Random objID
        $objects->objID = generateRandomID();

        // Number       
        if( is_numeric($value) ){
            $objects->$key = (float) $value;

        // Boolean
        } else if($value === 'true'){
            $objects->$key = true;
        } else if($value === 'false'){
            $objects->$key = false;

        // ClassName
        } else if ($key === 'className'){

        // String
        } else { $objects->$key = $value; }


        // createdOn & updatedOn Dates
        $objects->createdOn = array('type'=>'__date','date'=>$formattedDate);
        $objects->updatedOn = array('type'=>'__date','date'=>$formattedDate);

    }// ./ foreach


    // Save data into the JSON file
    $jsonStr = file_get_contents($className.'.json');
    // Decode the JSON string into a PHP array.
    $jsonObjs = json_decode($jsonStr, true);

    // Check if there's some new key and values -> update the $jsonObjs array
    $result = array_diff($jsonObjs, $objects);
    print_r('<br><br>DIFFERENCE: <br>'.$result[0].'<br>');

    // Push array       
    array_push($jsonObjs, $objects);
    // Encode the array back into a JSON string and save it.
    $jsonData = json_encode($jsonObjs);
    file_put_contents($className.'.json', $jsonData);
    // echo JSON data
    echo $jsonData;

我试图用array_diff() 来区分$jsonObjs$objects

 $result = array_diff($jsonObjs, $objects);
 print_r(.$result[0].'<br>');

但它不起作用,它显示一个空行,并且 error_log 文件也显示了这个:

PHP Warning:  array_diff(): Argument #2 is not an array on line 63

我启动了 2 个 URL 字符串,第一个以

开头
create.php?className=Users&string=lorem%20ipsum

在第二个中,我添加了一个额外的字符串对象,如下所示:

create.php?className=Users&string=lorem%20ipsum&username=johndoe

所以,我需要实现的是将“用户名”键也添加到第一个对象中,就像在第二个对象上一样。换句话说,我的Users.json 文件在启动第二个 URL 字符串后应该是这样的:

[
    {
      "objID":"Y0FVVFZYCV",
      "username":"", //<-- upodated key with no value
      "createdOn":{"type":"__date","date":"2018-09-21T16:48:09"},
      "string":"lorem ipsum"
    },
    {
      "objID":"YShAUqIcMg",
      "username":"johndoe", // new key->value here!
      "createdOn":{"type":"__date","date":"2018-09-21T16:48:14"},
      "string":"lorem ipsum"
    }
    ]

【问题讨论】:

  • 正如错误所说,第二个参数不是数组。您已经花了很多行将其视为一个对象,所以您应该知道它不是一个数组!
  • 好吧,我不知道,因为我的 array_push() 工作正常,所以我假设 $objects 是一个类似 $jsonObjs 的数组,否则 array_push() 怎么工作..?
  • $objects-&gt;key, $objects-&gt;createdOn 这不是数组语法。我看到的array_push() 的唯一实例被应用于$jsonObjs,这可能是一个数组。 (尽管值得一提的是,您没有从 json_decode() 进行任何错误检查,因此它可以很容易地成为 null。)
  • $object 是一个类的实例吗?从你的语法来看,它似乎是一个类。
  • 当我做 array_push($jsonObjs, $objects) 时,它会将 $objects 数据添加到 $jsonObjs PHP 数组中,所以我是 PHP 新手,我认为 $objects 是 PHP 数组同样,否则如何将 $objects 添加到 $jsonObjs 中? $objects 从我的浏览器启动的 URL 字符串中获取数据,例如 create.php?username=johndoe&string=lorem ipsum

标签: php json database


【解决方案1】:

正如 cmets 中已经指出的,您不能将“$objects”传递给 array_diff 函数,因为它需要一个数组,而“$objects”是一个对象。

可以通过这样调用将对象转换为数组:

$result = array_diff($jsonObjs, (array)$objects);

【讨论】:

  • 还没有,抱歉,因为它仍然没有回应,所以你的解决方案不起作用。也许我应该 json_decode)$objects)?
  • 不,即使我 json_decode() 它不起作用,它不会打印任何东西作为 $result
  • 如果您在 array_diff 调用之后立即使用 var_dump($result) 会显示什么?
  • 仍然没有,我想我必须将 stdClass 对象转换为 PHP 数组,如果我 print_r $objects 我得到:stdClass Object(...)
【解决方案2】:

正如我的@miken32 所指出的,我的 $objects 对象不是一个数组,而是一个 StdClass 对象,所以我只需要替换:

$objects->

与:

$objects[$key]

这样$objects就变成了一个有效的数组,我可以像这样使用array_diff():

$result = array_diff_assoc($jsonObjs, $objects);
print_r('DIFF: '.json_encode($result).'<hr>');

它打印出一个有效的 JSON 数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-21
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多