douxingti9307
2018-05-05 19:30
浏览 47

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

I'm unable to print JSON in a certain format. I was trying values in nested array format. My PHP code is

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

Which Outputs *********

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

But I want to print in the Following Format: In a format that will look like below.

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

How can I do in PHP?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanchuaiwan0063 2018-05-05 21:42
    已采纳

    I would suggest you take the room-identifieres as array-key an make use of array_key_exists in your loop. See example below:

    $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>";
    
    打赏 评论

相关推荐 更多相似问题