Skip to content
Advertisement

Php json_encode array and object array

I need to create a json with this format:

{
  "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"
      }
    }
  ]
}

in entries, a $header who check the user token(not use for my problem) $table the table returned from PDO::FETCHASSOC from sql SELECT request My php code:

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);
}

I have this result

{"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": ""
        }]
}

I think the Json format eror is in the array_merge function. What add array function can I use to not remove the Box object

Advertisement

Answer

Try this one

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);
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement