I will be quick with more code less talk.
I have a mySQL DB and i want to generate json from it with PHP and PDO.
<?php
require("db.php");
$dsn = "mysql:host=localhost;dbname=$dbname";
$pdo = new PDO($dsn, $username, $password);
$rows = array();
$stmt = $pdo->prepare("SELECT * FROM table");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo '{"jsons":';
echo json_encode($rows);
echo "}";
?>
The above code will return
{"jsons":[{"date_time":"2012-09-06 20:55:44","name":"theOne","message":"i am god","ip":"","device":"HTC","device_type":"Phone","class":"class1","id":"1"}]}
I want to modify the php code so my json output will be
{"jsons":[{"date_time":"2012-09-06 20:55:44","name":"theOne","message":"i am god","ip":"","devs":{"device":"HTC","device_type":"Phone","class":"class1","id":"1"}}]}
The difference is that i want to nest device
,device_type
,class
and id
into devs.
I couldn't do anything or find from google such a task.
Any help will be appreciate.
Thank you.