Reputation: 373
I am working on an API in PHP that sends JSON data in form of an array to an android app. I'm having issues in returning the JSON array in the format;
"sammury" : [
{ "ClientName": "Samuel", "OrdersTaken": 400 },
{ "ClientName": "James", "OrdersTaken": 20 },
{ "ClientName": "Ritah", "OrdersTaken": 9 }
]
I'm using a php server online and a mysql database. I have added an image showing how the data that is returned loks like. Any Assistance Will be highly appreciated
Upvotes: 1
Views: 51
Reputation: 1876
Here is a simple example for you how to send your desired response. 1) Create your object arrays 2) Push the objects into the data array (you can use loop to push objects to array) and then assign that data array to the response array and convert it to Json. Please see the working code here
$data = [];
$obj = [
"ClientName"=> "Samuel",
"OrdersTaken"=> 400
];
array_push($data, $obj);
$response = [
"sammury"=> $data
];
$json = json_encode($response);
echo $json;
Upvotes: 1