【问题标题】:auto-incrementing ID on my JSON array我的 JSON 数组上的自动递增 ID
【发布时间】:2018-05-30 13:50:29
【问题描述】:

我一直在尝试这样做一段时间,但我似乎无法做到这一点。 One post 让我非常接近但不是很接近,因为我的 JSON 的层次结构。 (这是一个任务,这个层次结构是强制性的。)

我现在要做的是从一个页面提交信息,将其发布到我在另一个页面上的 php 中,将其保存在一个数组中,json_encode 该数组并将其写入我的 JSON 文件。

这是我的淡化代码,希望能去掉大部分不必要的代码:

<?php

$filename = "json/exercises.json";
$filesize = filesize($filename);
$fp = fopen($filename, "r+");

#Accept the submitted form
$exLanguage = $_POST['exLanguage'];
$exTitle = $_POST['exTitle'];
$exStuff = $_POST['exStuff'];

#write to JSON with an incrementing ID
if (file_get_contents($filename) == "") {
  $exercise = array (
    "id" => 1,
    "lang" => $exLanguage,
    "title" => $exTitle,
    "main_object" =>
      [
      "exStuff" => $exStuff
      ]
  );
  $exercise_json = json_encode($exercise, JSON_PRETTY_PRINT);
  file_put_contents($filename, $exercise_json, FILE_APPEND);

} else {

  #Get the last set ID
  $jsonid = json_decode(fread($fp, $filesize), true);
  $last = end($jsonid);
  $title = prev($jsonid);
  $lang = prev($jsonid);
  $id = prev($jsonid);

  $exercise = array (
    "id" => ++$id,
    "lang" => $exLanguage,
    "title" => $exTitle,
    "main_object" =>
      [
      "exStuff" => $exStuff
      ]
  );
  $exercise_json = json_encode($exercise, JSON_PRETTY_PRINT);
  file_put_contents($filename, $exercise_json, FILE_APPEND);
}

?>

现在,如果我的 json 为空,它会正确添加第一个数组,ID 为 1。然后,如果我尝试再次添加到我的 json,它会正确添加它,ID 为 2。但任何更多尝试写入到 JSON 会给我这些错误:

警告:end() 期望参数 1 为数组,给定为空

警告:prev() 期望参数 1 是数组,给定 null

警告:prev() 期望参数 1 是数组,给定 null

警告:prev() 期望参数 1 是数组,给定 null

我试过了

$reset = reset($jsonid);

在写入文件后但没有成功,只是在第三次写入时给了我另一个错误,因为重置也被赋予了空值。

谁能告诉我如何使它也能正常工作?或者是否有更简单的方法来完成这项工作?

【问题讨论】:

  • 这是一个 50:50 的猜测。你$fp = fopen($filename, "r+"); 使得$fp 和文件的一行一行的数组。那么你$jsonid = json_decode(fread($fp, $filesize), true);$fp 仍然是一个数组。我认为这行不通。我认为$jsonid = json_decode(file_get_contents($filename), true); 可能有用。

标签: php arrays json auto-increment


【解决方案1】:

首先了解 JSON 格式。它是对象的集合,这意味着 attribute: value 对包裹在 {} 中并用逗号分隔 , 然后再次包裹在 {}[] 中。

您的 JSON 结构不完整或已损坏。您当前正在执行的操作将在您的 JSON 文件中创建以下格式的 JSON:

{ "id": 1, "lang": "as", "title": "asdsad" }
{ "id": 2, "lang": "as", "title": "asdsad" }
{ "id": 3, "lang": "as", "title": "asdsad" }
{ "id": 4, "lang": "as", "title": "asdsad" }

所以 json_decode 在这种情况下将返回 null,因为 JSON 格式无效。

您需要继续在现有 JSON 中附加新的 JSON,这样上述格式将变成这样:

[
    { "id": 1, "lang": "as", "title": "asdsad" },
    { "id": 2, "lang": "as", "title": "asdsad" },
    { "id": 3, "lang": "as", "title": "asdsad" },
    { "id": 4, "lang": "as", "title": "asdsad" }
]

这意味着您的 else 块不正确。在else 块中使用以下代码:

$jsonid = json_decode(file_get_contents($filename), true);
$last = end($jsonid);
$id = $last['id'];

$jsonid[] = array (
    "id" => ++$id,
    "lang" => $exLanguage,
    "title" => $exTitle,
    "main_object" => array("exStuff" => $exStuff)
);
$exercise_json = json_encode($jsonid, JSON_PRETTY_PRINT);
file_put_contents($filename, $exercise_json, FILE_APPEND);

我希望您不必使用不正确/损坏的 JSON 格式。在任何情况下,这都不适用于json_decode()

【讨论】:

  • 嗯,这给了我相同的结果,只是没有所有的prevs。我第二次写入 id 2,第三次写入 id 1,警告警告:end() 期望参数 1 为数组,给定 null
  • 对不起,我的错误。我在编码 json 时使用了错误的变量。它应该是 $jsonid 而不是 $exercise。代码已更新。立即检查。
猜你喜欢
  • 2015-03-10
  • 1970-01-01
  • 2015-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-04
  • 1970-01-01
  • 2014-09-10
相关资源
最近更新 更多