I create my api restoration in symfony 4, but the array I return does not look like I want to.
Below I present my code, resultalt that is currently being performed and at the bottom is the result that I am trying to do.
Can I ask someone for help? I am not entirely sure how to achieve this effect.
This is my service php:
$data = $repository->findByData($fromDate, $toDate);
$i = 1;
$j = 1;
$resultArray = [];
foreach ($data as $key => $d) {
$date = $d['date'];
$dateString = $date->format('Y-m-d');
$resultIteration[$key] = [
'customerID' => $d['id'],
'customerName' => $d['customerName'],
'data' => [
'ordersAmount' . $i++ => $d['ordersAmount'],
'earnings' . $i++ => $d['earnings'],
'date' => $dateString,
],
'ordersAmount' . $j++ => $d['ordersAmount'],
'earnings' . $j++ => $d['earnings'],
];
array_push($resultArray,$resultIteration[$key]);
}
return $resultArray;
Current result:
[
{
"customerID": 1,
"customerName": "Client1",
"data": {
"ordersAmount1": 10,
"earnings2": 882.53,
"date": "2019-02-21"
},
"ordersAmount1": 10,
"earnings2": 882.53
},
{
"customerID": 1,
"customerName": "Client1",
"data": {
"ordersAmount3": 18,
"earnings4": 21,
"date": "2019-02-20"
},
"ordersAmount3": 18,
"earnings4": 21
},
{
"customerID": 2,
"customerName": "Client2",
"data": {
"ordersAmount5": 43,
"earnings6": 11,
"date": "2019-02-21"
},
"ordersAmount5": 43,
"earnings6": 11
},
{
"customerID": 2,
"customerName": "Client2",
"data": {
"ordersAmount7": 21,
"earnings8": 41,
"date": "2019-02-20"
},
"ordersAmount7": 21,
"earnings8": 41
}
]
I would like to this result:
[
{
customerID: 1,
customerName: "Client1",
data: [
{
date: "2019-02-20"
},
{
date: "2019-02-21"
}
],
ordersAmount1: 12,
earnings2: 3333,
ordersAmount3: 1,
earnings4: 2,
},
{
customerID: 2,
customerName: "Client2",
data: [
{
date: "2019-02-20"
},
date: "2019-02-21"
}
],
ordersAmount1: 3112,
earnings2: 44,
ordersAmount3: 121,
earnings4: 2543,
}
],
Edit: this is my $data:
[
{
"date": "2019-02-21T00:00:00+01:00",
"ordersAmount": 10,
"earnings": 882.53,
"id": 1,
"customerName": "Client1"
},
{
"date": "2019-02-20T00:00:00+01:00",
"ordersAmount": 18,
"earnings": 21,
"id": 1,
"customerName": "Client1"
},
{
"date": "2019-02-21T00:00:00+01:00",
"ordersAmount": 43,
"earnings": 11,
"id": 2,
"customerName": "Client2"
},
{
"date": "2019-02-20T00:00:00+01:00",
"ordersAmount": 21,
"earnings": 41,
"id": 2,
"customerName": "Client2"
}
]