【问题标题】:Update JSON value if exists otherwise add it in PHP如果存在则更新 JSON 值,否则将其添加到 PHP 中
【发布时间】:2014-03-06 23:19:38
【问题描述】:

我有一个 JSON 文件,其中包含以下内容:

{"faqitem": [{ "id": "faq1", "question": "Question 1"}]}

我正在尝试做两件事。如果存在则更新特定值,如果不存在则添加新值。

目前,我可以更新文件,但它只是不断添加新值,如果已经存在则永远不会更新。

$faqpage = 'includes/faq.json';

$file = file_get_contents($faqpage);    
$obj = json_decode($file); 

$newdata['id'] = "faq2";
$newdata['question'] = "This is the second question";

foreach($obj->faqitem as $key => $val ) {
   echo "IDS " . $val->id . " = " . $newdata['id'] . "<br/>\n";
   if ($val->id == $newdata['id']) {
     $val->question = $newdata['question'];
     echo $val->id . "<br/>match<br/>";
   } else {
         $newstuff = new stdClass;
     $newstuff->id = $newdata['id'];
     $newstuff->question = $newdata['question'];     
     array_push($obj->faqitem, $newstuff);
     echo "<br/>no match<br/>";
   }
}

echo json_encode($obj);

$fh = fopen($faqpage, 'w') or die ("can't open file");  
//okay now let's open our file to prepare it to write
fwrite($fh, json_encode($obj));
fclose($fh);

这是一个带有重复对象 ID 的示例输出:

{"faqitem":[{"id":"faq1","question":"Question 1"},{"id":"faq2","question":"This is the updated question"},{"id":"faq2","question":"This is the updated question"}]}

【问题讨论】:

  • 尝试使用“by reference”,如foreach($obj-&gt;faqitem as $key =&gt; &amp;$val ) {
  • 这没什么区别,我仍然在文件中得到重复的值。 {"faqitem":[{"id":"faq1","question":"Question 1"},{"id":"faq2","question":"This is the updated question"},{"id":"faq2","question":"This is the updated question"}]}
  • 公平地说,通过eval.in/114925 运行它似乎工作正常......在您的示例中,您的数据 is 不同,因此它应该添加一个新密钥。如果将 $newdata 数组中的 faq2 更改为 faq1,它确实会被覆盖。
  • 它可以很好地写入文件,但是,假设我有一个 ID 为“faq2”的对象。它没有更新该对象,而是附加了一个 ID 为“faq2”的新对象。我不希望它那样工作。如果 id 存在则需要更新。
  • 是的,很酷,您能否在您的问题中包含一个示例,因为您提供的内容很好。

标签: php json


【解决方案1】:

您的问题是您的逻辑不正确。在第一次迭代中,ID 不匹配,因此将添加 $newdata。在第二次迭代中,ID 匹配并且项目将被更新 - 但请稍等。我们刚刚在之前的迭代中添加了这个项目!所以你的循环部分应该是这样的:

...
$exists = false;
foreach($obj->faqitem as $key => $val)
{
    // update if exists
    if($val->id == $newdata['id']) {
        $val->question = $newdata['question'];
        $exists = true;
    }
}

// add new if not exists
if(!$exists) {
    $newstuff = new stdClass;
    $newstuff->id = $newdata['id'];
    $newstuff->question = $newdata['question'];     
    array_push($obj->faqitem, $newstuff);
}
...

【讨论】:

  • 美丽,这完全正确。感谢您的帮助。
【解决方案2】:

因此,将整个 JSON 方面排除在外。那只是序列化。想想如何使数据结构看起来像你想要的那样。想想你的数据结构。对我来说,这似乎不是你的目的的最佳结构,也许你需要你的 id 作为查找键,所以当你向数据结构中添加一个项目时,你要么在该 id 键处创建一个新值或覆盖该键的值。

如果您的数据结构如下:

{"faqitem":
    {
        "faq1": "Faq content",
        "faq2": "Faq 2 content"
    }
}

那么你的代码代码就这么简单:

$faqpage = 'includes/faq.json';

$file = file_get_contents($faqpage);    
$obj = json_decode($file); 

$newdata['id'] = "faq2";
$newdata['question'] = "This is the second question";

$obj->faqitem->{$newdata['id']} = $newdata['question'];

echo json_encode($obj);

$fh = fopen($faqpage, 'w') or die ("can't open file");  
//okay now let's open our file to prepare it to write
fwrite($fh, json_encode($obj));
fclose($fh);

【讨论】:

    猜你喜欢
    • 2011-10-16
    • 2018-05-04
    • 2020-03-12
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    相关资源
    最近更新 更多