将JSON响应转换为特定格式?

时间:2018-05-05 19:30:49

标签: php json while-loop

我无法以某种格式打印JSON。我正在尝试嵌套数组格式的值。 我的PHP代码是

while ($stmtPGBeds->fetch()) {
    $PGBEDS[$count] = array('pgbedid' => $bedid, 'roomnum'=>$roomnum, 
     'roomtype'=>$roomtype, 'spacetype'=>$spacetype, 'price'=>$price,
     'deposit'=>$deposit, 'status'=>$status, 'assetid'=>$assetid);
    $count++;
}

哪个输出*********

"beds": [
        {
            "bedid": "PGB050418154448673",
            "roomnum": "1",
            "roomtype": "Bedroom",
            "spacetype": "Single",
            "price": "7500",
            "deposit": "10000",
            "status": "0",
            "assetid": "AST050418051246344"
        },
        {
            "bedid": "PGB050418154448119",
            "roomnum": "2",
            "roomtype": "Bedroom",
            "spacetype": "Sharing",
            "price": "5500",
            "deposit": "10000",
            "status": "0",
        }
]

但我想以下列格式打印: 格式如下所示。

"beds": [
        {
            "roomnum": "1",
            "roomtype": "Bedroom",
            "spacetype": "Single",
            "assetid": "AST050418051246344"
            "beds": [
                  {
                        "bedid": "PGB050418154448673",
                        "price": "7500",
                        "deposit": "10000",
                        "status": "0",
                  },
                  {
                        "bedid": "PGB050418154448673",
                        "price": "7500",
                        "deposit": "10000",
                        "status": "0",
                  }
             ]
        },
        {
            "roomnum": "2",
            "roomtype": "Bedroom",
            "spacetype": "Single",
            "assetid": "AST050418051246344"
            "beds": [
                  {
                        "bedid": "PGB050418154448673",
                        "price": "7500",
                        "deposit": "10000",
                        "status": "0",
                  },
                  {
                        "bedid": "PGB050418154448673",
                        "price": "7500",
                        "deposit": "10000",
                        "status": "0",
                  }
             ]
        }
]

我该怎么做PHP?

1 个答案:

答案 0 :(得分:0)

我建议你把房间标识作为数组键,在你的循环中使用array_key_exists。见下面的例子:

$response_data = array(
    array("roomnum"=>1, "bedid"=>1, "roomtype"=>"bedroom", "spacetype"=>"single", "price"=>7500, "desposit"=>10000, "status"=>0, "asset_id"=>"AST050418051246344"),
    array("roomnum"=>1, "bedid"=>2, "roomtype"=>"bedroom", "spacetype"=>"single", "price"=>7500, "desposit"=>10000, "status"=>0, "asset_id"=>"AST050418051246345"),
    array("roomnum"=>2, "bedid"=>1, "roomtype"=>"bedroom", "spacetype"=>"single", "price"=>8500, "desposit"=>10000, "status"=>0, "asset_id"=>"AST050418051246346"),
    array("roomnum"=>2, "bedid"=>2, "roomtype"=>"bedroom", "spacetype"=>"single", "price"=>8500, "desposit"=>10000, "status"=>0, "asset_id"=>"AST050418051246347"),
    array("roomnum"=>2, "bedid"=>3, "roomtype"=>"bedroom", "spacetype"=>"single", "price"=>8500, "desposit"=>10000, "status"=>0, "asset_id"=>"AST050418051246348")
);

$output = array();
foreach ($response_data as $value) 
{
    if ( ! array_key_exists($value["roomnum"], $output))
    {
        // lets create a new room 
        $output[$value["roomnum"]] = array(
            "roomnum"=>$value["roomnum"], 
            "roomtype"=>$value["roomtype"], 
            "spacetype"=>$value["spacetype"],
            // ... insert all your room informations here
            // ... 
            "beds"=>array()  // <<-- and create an empty array to store your beds later     

        );
    }

    // now create the array containing all the bed informations
    $bed_data = array(
        "bedid"=>$value["bedid"], 
        "price"=>$value["price"],
        "desposit"=>$value["desposit"],
        "status"=>$value["status"]
    );


    // ... and push it to the already created beds array 
    $output[$value["roomnum"]]["beds"][] = $bed_data; 
}

echo "<pre>".print_r($output, true)."</pre>";