Here is my PHP json output
[ {
"campaign_id": "4",
"date": "2017-04-01",
"name": "Dealoyal",
"iteration": "5149"
},
....
]
I want to make it to look like this:
[{
"date": "2017-04-01",
"Dealoyal": "5149"
},...]
My code:
foreach ($result as $row) {
$name[] = $row['name'];
$iteration[] = $row['iteration'];
}
$mine[]=array_combine($name, $iteration);
echo json_encode($mine,JSON_PRETTY_PRINT);
But this only prints the last day names and iterations , when I need all days that I choose at date range. Do I need to use for loop or maybe there is another way?
You don't need to use array_combine()
, you can directly create $mine
in the foreach
:
$mine = array();
foreach ($result as $row) {
$mine[] = array(
'date' => $row['date'],
$row['name'] => $row['iteration'],
);
}
echo json_encode($mine, JSON_PRETTY_PRINT);