【问题标题】:Php json_encode array and object arrayphp json_encode 数组和对象数组
【发布时间】:2020-06-16 20:20:22
【问题描述】:

我需要用这种格式创建一个json:

{
  "reservs": [
    {
      "ResId": "58",
      "time": "2020-05-15 19:41:50",
      "boxEntering": null,
      "boxClosing": null,
      "active": "1",
      "UserId": "29",
      "BoxId": "4",
      "boxPlace": null,
      "box": {
        "id": "4",
        "Nom": "Hortillonages",
        "Lat": "49.8953",
        "Lng": "2.31034",
        "place": "0",
        "placeMax": "9"
      }
    }
  ]
}

在条目中,一个检查用户令牌的 $header(不用于我的问题) $table 从 sql SELECT 请求的 PDO::FETCHASSOC 返回的表 我的php代码:

function generateJson($table, headerChecker $header){
    $final = array();
    foreach ($table as $item) {
        $box = array(
            "id" => $item["id"],
            "Nom" => $item["Nom"],
            "Lat" => $item["Lat"],
            "Lng" => $item["Lng"],
            "place" => $item["place"],
            "placeMax" => $item["placeMax"]
        );
        $reserv = array(
            "ResId" => $item["ResId"],
            "time" => $item["time"],
            "boxEntering" => $item["boxEntering"],
            "boxClosing" => $item["boxClosing"],
            "active" => $item["active"],
            "UserId" => $item["UserId"],
            "BoxId" => $item["BoxId"],
            "boxPlace" => $item["boxPlace"],
        );
        $reserv["box"] = $box;
        array_merge($final,$reserv);
    }
    $arr = array("reservs" => $table);
    $header->tokenJson($arr);
    echo json_encode($arr);
}

我有这个结果

{"reservs": [
        {
            "ResId": "58",
            "time": "2020-05-15 19:41:50",
            "boxEntering": null,
            "boxClosing": null,
            "active": "1",
            "UserId": "29",
            "BoxId": "4",
            "boxPlace": null,
            "id": "4",
            "Nom": "Hortillonages",
            "Lat": "49.8953",
            "Lng": "2.31034",
            "place": "0",
            "placeMax": "9",
            "QRID": "",
            "boxToken": ""
        }]
}

我认为 Json 格式错误在 array_merge 函数中。 我可以使用什么添加数组函数来不删除 Box 对象

【问题讨论】:

  • 确保您了解哪个变量包含哪个值。
  • 不是问题,但你最好使用$final[] = $reserv;
  • 在将数组分配给它之前使用$reserv['box'] = []; ,或者将其转换为数组。

标签: php arrays json add


【解决方案1】:

您的问题是您没有分配array_merge 的返回值并使用了错误的变量$table。只需动态追加到$final

foreach ($table as $item) {
    // this all appears good
    //
    $reserv["box"] = $box;
    $final[] = $reserv;             // append to $final
}
$arr = array("reservs" => $final);  // use $final
$header->tokenJson($arr);
echo json_encode($arr);

将新的$reserv合并到$final并赋值给$final,然后使用$final

【讨论】:

  • 有效!谢谢你。很早的错误:'(但是现在有多个表数据,json输出只有一次迭代
  • 已修复。我尝试使用您的代码,但 array_merge 不正确。
【解决方案2】:

试试这个

function generateJson($table, headerChecker $header){
    $final = array();
    foreach ($table as $item) {
        $box = array(
            "id" => $item["id"],
            "Nom" => $item["Nom"],
            "Lat" => $item["Lat"],
            "Lng" => $item["Lng"],
            "place" => $item["place"],
            "placeMax" => $item["placeMax"]
        );
        $reserv = array(
            "ResId" => $item["ResId"],
            "time" => $item["time"],
            "boxEntering" => $item["boxEntering"],
            "boxClosing" => $item["boxClosing"],
            "active" => $item["active"],
            "UserId" => $item["UserId"],
            "BoxId" => $item["BoxId"],
            "boxPlace" => $item["boxPlace"],
        );
        $reserv["box"] = $box;
        $final[] = $reserv;
    }
    $arr = array("reservs" => $final);
    $header->tokenJson($arr);
    echo json_encode($arr);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-25
    • 2012-07-29
    • 2021-12-13
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多