简体   繁体   中英

How create a response json with nested objects in php?

I want create a json response with nested json object and group my actual results by id_hotel

在此处输入图片说明

MY ACTUAL JSON RESPONSE

 {
    "tag": "cities",
    "success": 1,
    "error": 0,
    "items": 2,
    "item": [
        {
            "id": "6194",
            "year": "2013",
            "city": "London",
            "start": "1",
            "id_hotel": "20001",
            "name": "hotel piccadilly",
            "address": "road 4",
            "number_fr": "7003",
            "district": "london city",
            "pr": "GB",
            "fres": "1",
            "mode": "Night",
            "pos": "402",
            "pes": "33",
            "pis": "21",
            "pus": "456"
        },
        {
            "id": "6194",
            "year": "2013",
            "city": "London",
            "start": "1",
            "id_hotel": "20001",
            "name": "hotel piccadilly",
            "address": "road 4",
            "number_fr": "7003",
            "district": "london city",
            "pr": "GB",
            "fres": "1",
            "mode": "Day",
            "pos": "0",
            "pes": "1",
            "pis": "0",
            "pus": "1"
        }
    ]
}

MY PHP CODE for the encoding response

        $query = "SELECT * FROM cities WHERE city = 'London'";
    $result = mysql_query($query) or die(mysql_error());
    // check for empty result
    if (mysql_num_rows($result) > 0) {

        $response["item"] = array();

        while ($row = mysql_fetch_array($result)) {
            // temp user array
            $product = array();
            $product["id"] = $row["id"];
            $product["year"] = $row["year"];
            $product["city"] = $row["city"];
            $product["start"] = $row["start"];
            $product["id_hotel"] = $row["id_hotel"];
            $product["name"] = $row["name"];
            $product["address"] = $row["address"];
            $product["number_fr"] = $row["number_fr"];
            $product["district"] = $row["district"];
            $product["pr"] = $row["pr"];
            $product["fres"] = $row["fres"];
            $product["mode"] = $row["mode"];
            $product["pos"] = $row["pos"];
            $product["pes"] = $row["pes"];
            $product["pis"] = $row["pis"];
            $product["pus"] = $row["pus"];              

            // push single product into final response array
            array_push($response["item"], $product);
        }
        // success
        $response["items"] = mysql_num_rows($result);
        $response["success"] = 1;

        // echoing JSON response
        echo json_encode($response);

What I desider

    {
"tag": "cities",
"success": 1,
"error": 0,
"items": 2,
"item": [
    {
        "id": "6194",
        "year": "2013",
        "city": "London",
        "start": "1",
        "id_hotel": "20001",
        "name": "hotel piccadilly",
        "address": "road 4",
        "number_fr": "7003",
        "district": "london city",
        "pr": "GB",
        "fres": "1",
        "mode": [{
        "value" : "Night",
        "pos": "402",
        "pes": "33",
        "pis": "21",
        "pus": "456"
        },
       { "value" : "Day",
        "pos": "0",
        "pes": "1",
        "pis": "0",
        "pus": "1"
        }]

    }
]
}

thanks in advance for your attention!!

Add option JSON_FORCE_OBJECT in function. You can read about it in http://tr2.php.net/manual/en/json.constants.php . json_encode($response, JSON_FORCE_OBJECT) ;

1st, I suggest you a small change to save you some lines, and thus, i add some code to achieve what you want

 $query = "SELECT * FROM cities WHERE city = 'London'";
    $result = mysql_query($query) or die(mysql_error());
    // check for empty result
    if (mysql_num_rows($result) > 0) {

        $response["item"] = array();

       $current_id = NULL;

        while ($row = mysql_fetch_array($result)) {  
            if ( $row->id_hotel !== $current_id ){
                $response["item"][] = $row;
                $response["item"]["mode"] = array();
            }
            $current_item = end($response["item"]);
            $current_item["mode"][] = array("value"=>$row->value, "pes"=>$row->pes); 
            $current_id = $row->id_hotel;
        }
        // success
        $response["items"] = mysql_num_rows($result);
        $response["success"] = 1;

        // echoing JSON response
        echo json_encode($response);

Let me know if it works.

Note: Only used 1 variable for mode, for testing purposes, and didn´t unset the variables that I put into mode array, but should be enough to see if the main idea works. Feeling a bit lazy :P

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM