用PHP创建JSON对象的样式
我的JSON当前看起来像这样用PHP创建JSON对象的样式,php,json,Php,Json,我的JSON当前看起来像这样 { "customers": [ { "customer_id":3, "customer_name":"Rick", "Address":"333 North Road" }, { "customer_id":4, "customer_name":"Robby", "Address":"444 North West Road" }
{
"customers":
[
{
"customer_id":3,
"customer_name":"Rick",
"Address":"333 North Road"
},
{
"customer_id":4,
"customer_name":"Robby",
"Address":"444 North West Road"
}
]
}
我希望它看起来像这样
{
"customers":
[
{
"customer":
{
"customer_id":3,
"customer_name":"Rick",
"Address":"333 North Road"
}
},
{
"customer":
{
"customer_id":4,
"customer_name":"Robby",
"Address":"444 North West Road"
}
}
]
}
它是在这个php脚本中创建的,但我不确定如何以编程方式将customer属性添加到每个JSON对象中。请帮忙
//populate results
$json = array();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$array = array(
'customer_id' => $row['CustomerID'],
'customer_name' => $row['Name'],
'Address' => $row['Address']
);
array_push($json, $array);
foreach ($row as $r) {
}
}
$jsonstring = '{"customers":'. json_encode($json). "}";
return $jsonstring;
//填充结果
$json=array();
$result=$stmt->get_result();
而($row=$result->fetch_assoc()){
$array=array(“客户”=>array(//$row['CustomerID']),
“客户名称”=>$row[“名称”],
'Address'=>$row['Address']
)
);
array\u push($json,$array);
foreach($r行){
}
}
$jsonstring='{“客户”:.json_编码($json)。“}”;
返回$jsonstring;
$array=array(“客户”=>array(…)
但是你为什么要更改它,第一个是更好的。我正在尝试使语法与另一个问题的解决方案相匹配。我必须尝试消除可能性。必须等待计时器启动。
//populate results
$json = array();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$array = array("customer" => array( // <-- change is here
'customer_id' => $row['CustomerID'],
'customer_name' => $row['Name'],
'Address' => $row['Address']
)
);
array_push($json, $array);
foreach ($row as $r) {
}
}
$jsonstring = '{"customers":'. json_encode($json). "}";
return $jsonstring;