Modify generated json from PDO and PHP

Jexexhaum

New Member
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.\[code\]<?phprequire("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 "}";?>\[/code\]The above code will return\[code\]{"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"}]}\[/code\]I want to modify the php code so my json output will be\[code\]{"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"}}]}\[/code\]The difference is that i want to nest \[code\]device\[/code\],\[code\]device_type\[/code\],\[code\]class\[/code\] and \[code\]id\[/code\] into devs.I couldn't do anything or find from google such a task.Any help will be appreciate.Thank you.
 
Top