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

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题