Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将JSON响应转换为特定格式?_Php_Json_While Loop - Fatal编程技术网

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

Php 将JSON响应转换为特定格式?,php,json,while-loop,Php,Json,While Loop,我无法以某种格式打印JSON。我正在尝试嵌套数组格式的值。 我的PHP代码是 while ($stmtPGBeds->fetch()) { $PGBEDS[$count] = array('pgbedid' => $bedid, 'roomnum'=>$roomnum, 'roomtype'=>$roomtype, 'spacetype'=>$spacetype, 'price'=>$price, 'deposit'=>$d

我无法以某种格式打印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?

我建议您将房间标识作为数组键,并利用循环中存在的
数组键。见下例:

$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>";
$response\u data=array(
数组(“roomnum”=>1,“bedid”=>1,“roomtype”=>“卧室”,“spacetype”=>“单人房”,“价格”=>7500,“desposit”=>10000,“状态”=>0,“资产id”=>“AST050418051246344”),
数组(“roomnum”=>1,“bedid”=>2,“roomtype”=>“卧室”,“spacetype”=>“单人房”,“价格”=>7500,“desposit”=>10000,“状态”=>0,“资产id”=>“AST050418051246345”),
数组(“roomnum”=>2,“bedid”=>1,“roomtype”=>“卧室”,“spacetype”=>“单人房”,“价格”=>8500,“desposit”=>10000,“状态”=>0,“资产id”=>“AST050418051246346”),
数组(“roomnum”=>2,“bedid”=>2,“roomtype”=>“卧室”,“spacetype”=>“单人房”,“价格”=>8500,“desposit”=>10000,“状态”=>0,“资产id”=>“AST050418051246347”),
数组(“roomnum”=>2,“bedid”=>3,“roomtype”=>“卧室”,“spacetype”=>“单人房”,“价格”=>8500,“desposit”=>10000,“状态”=>0,“资产id”=>“AST050418051246348”)
);
$output=array();
foreach($response\u数据为$value)
{
如果(!array_key_存在($value[“roomnum”],$output))
{
//让我们创建一个新房间
$output[$value[“roomnum”]=array(
“roomnum”=>$value[“roomnum”],
“roomtype”=>$value[“roomtype”],
“spacetype”=>$value[“spacetype”],
//…在此处插入您的所有房间信息
// ... 
“床”=>array()/$value[“价格”],
“desposit”=>$value[“desposit”],
“状态”=>$value[“状态”]
);
//…并将其推送到已创建的床阵列
$output[$value[“roomnum”][“beds”][]=$bed\u数据;
}
echo“.print_r($output,true)。”;

您尝试了什么?您需要创建一个新数组,遍历现有数组,并将每个条目排序到父/子关系中。非常好。谢谢。