I am trying to produce the following JSON from MySQL database using PHP. How do I go about doing it in PHP lets say using the explode function for getting the array into the JSON object. I don't know about getting the JSON object inside an object. I just need to separate PHP files to achieve the following.
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM users';
$q = $pdo->prepare($sql);
$q->execute(array($sql));
$array = array();
while ($row = $q->fetch(PDO::FETCH_ASSOC)){
array_push($array, $row);
}
$json = json_encode($array);
echo $json;
Database::disconnect();?>
Array inside a JSON object:
[
{
"firstName":"John",
"lastName":"Doe",
"images": ['image1','image2','image3']
},
{
"firstName":"Anna",
"lastName":"Smith",
"images": ['image1','image2','image3']
},
{
"firstName":"Peter",
"lastName":"Jones",
"images": ['image1','image2','image3']
}
]
JSON object inside an object:
[
{
"firstName":"John",
"lastName":"Doe",
"cover": {
"cover_id": "0858699703",
"source": "www.myimages.co.zw/images/photo",
"offset_y": "0"
}
},
{
"firstName":"Anna",
"lastName":"Smith"
"cover": {
"cover_id": "0858699703",
"source": "www.myimages.co.zw/images/photo",
"offset_y": "0"
}
},
{
"firstName":"Peter",
"lastName":"Jones"
"cover": {
"cover_id": "0858699703",
"source": "www.myimages.co.zw/images/photo",
"offset_y": "0"
}
}
]
json_encode()on your actual result set