douxingti9307 2018-05-05 19:30
浏览 50
已采纳

将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>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退