JavaScript:
var chartData = [];
for (var i = 0; i< data.prices.length; i++)
{
chartData.push
({
"date": data.prices[i][0],
"value": data.prices[i][1],
"volume": data.total_volumes[i][1]
});
}
My attempt (PHP):
$chartData = [];
$length = count($data->prices);
for ($i = 0; $i < $length; $i++)
{
$chartData[] = array($data->prices[$i][0],$data->prices[$i][1],$data->total_volumes[$i][1]);
}
I'm trying to handle data from an API server side with php and then encode the data for javascript. I'm stuck converting this javascript snippet to PHP at the "date": "value": and "volume": I don't know what the equivalent to this would be in PHP. It looks as though they are labels in javascript to the data being pushed to the array? How do I give the data being pushed to the array the same labels in PHP?
You were pretty close you can define keys and values in PHP by doing:
$chartData[] = array(
"date" => $data->prices[$i][0],
"value" => $data->prices[$i][1],
"volume" => $data->total_volumes[$i][1]
);
Here's a "clever" way:
$chartData = array_map(function($price,$volume) {
return array(
"date" => $price[0],
"value" => $price[1],
"volume" => $volume[1]
);
}, $data->prices, $data->total_volumes);