我正在尝试使用PDO发送一组查询,然后返回一个多对象JSON响应,并且一切正常,除了json_encode返回具有索引号的对象,而且我找不到一种方法来返回命名对象,以便以后在javascrip中将其用作
data.address.StreetName
喜欢:
{ "adddress": { "StreetName": "bla bla bla", "number": "123" } "device": { "deviceName": "bla bla bla", "deviceID": "123" } }
这是我的php代码
$stmt = $db_con->prepare($q); $stmt->execute() $address = $stmt->fetchAll(PDO::FETCH_ASSOC); $stmt = $db_con->prepare($q); $stmt->execute() $device = $stmt->fetchAll(PDO::FETCH_ASSOC); $json = json_encode(array($address,$device));
我目前正在
[["StreetName": "bla bla bla", "number": "123"], ["deviceName": "bla bla bla", "deviceID": "123"] ]
答案 0 :(得分:2)
添加密钥:
$json = json_encode(array('address' => $address, 'device' => $device));
或者:
$json = json_encode(compact('address', 'device'));