用PHP创建JSON对象的样式


create style of JSON object with PHP

我的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":
             {
                 "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;
//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;