I'm trying to send a set of queries with PDO and then return a multi-object JSON response and everything is working fine except for json_encode is returning my objects with index number and I can't find a way to return as named objects so I can use it later in javascrip as
data.address.StreetName
like:
{ "adddress": { "StreetName": "bla bla bla", "number": "123" } "device": { "deviceName": "bla bla bla", "deviceID": "123" } }
this is my php code
$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));
I'm currently getting
[["StreetName": "bla bla bla", "number": "123"], ["deviceName": "bla bla bla", "deviceID": "123"] ]
Add the keys:
$json = json_encode(array('address' => $address, 'device' => $device));
Or:
$json = json_encode(compact('address', 'device'));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.