4
I have a CSV that returns me the following array:
Array
(
[0] => Array
(
[0] => ANO
[1] => Sciences
[2] => Mechanics
[3] => Telecom
)
[1] => Array
(
[0] => 2001
[1] => 226
[2] => 27
[3] => 81
)
[2] => Array
(
[0] => 2002
[1] => 433
[2] => 59
[3] => 162
)
[3] => Array
(
[0] => 2003
[1] => 816
[2] => 165
[3] => 647
)
[4] => Array
(
[0] => 2004
[1] => 1098
[2] => 421
[3] => 864
)
)
What I need to turn this array into a json with the following format:
"categories": [
{
"category": [{"label": "2001"}, {"label": "2002"}, {"label": "2003"}, {"label": "2004"}]
}
],
"dataset": [
{
"seriesname": "Sciences",
"data": [{"value": "226"}, {"value": "433"}, {"value": "816"}, {"value": "1098"}]
},
{
"seriesname": "Mechanics",
"data": [{"value": "27"}, {"value": "59"}, {"value": "165"}, {"value": "421"}]
},
{
"seriesname": "Telecom",
"data": [{"value": "81"}, {"value": "162"}, {"value": "647"}, {"value": "864"}]
}
]
I’m cracking my head on this, even though I know it’s not so complex, but I’m really stuck =/
EDIT 1
Follow what I’m doing:
for ($i=0; $i < count($spreadsheet_data); $i++) {
$chart['categories']['category'][]['label'] = array_shift($spreadsheet_data[$i]);
for ($d=0; $d < count($spreadsheet_data[$i]); $d++) {
$chart['dataset']['data'][]['value'] = $spreadsheet_data[$i][$d];
}
}
Put part of your code, so we can suggest changes, to solve the problem.
– David
I added the loop where I handle the array
– Eduardorph
Can’t do exactly what you want not to have set multiple keys with the same name for example
label
andvalue
would have to be a numerical Vault in this case.– rray