0

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

3
  • 1
    Make sure you understand which variable holds which value. Commented Jun 16, 2020 at 20:22
  • Not the problem, but you may be better off using $final[] = $reserv; Commented Jun 16, 2020 at 20:24
  • use $reserv['box'] = []; before you assigning the array to it, or cast it as array. Commented Jun 16, 2020 at 20:35

2 Answers 2

1

Your problem is that you are not assigning the return of array_merge and using the wrong variable $table. Just dynamically append to $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);

Merge the new $reserv into $final and assign it to $final, then use $final.

Sign up to request clarification or add additional context in comments.

2 Comments

It works! Thank you. Very early error: '( But now with multiple table data, the json output just one iteration
Fixed. I tried to use your code, but array_merge was not correct.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.