I am trying to get my data array in a format that is used by Highcharts to generate a multi-series chart of the data.
My data array is:
Array (
[answer1] => Array ( [SubQuestion 1] => 3 [SubQuestion 2] => 1 )
[answer2] => Array ( [SubQuestion 1] => 2 [SubQuestion 2] => 2 )
[answer3] => Array ( [SubQuestion 1] => 1 [SubQuestion 2] => 1 )
[answer4] => Array ( [SubQuestion 1] => 1 [SubQuestion 2] => 2 )
[answer5] => Array ( [SubQuestion 1] => 0 [SubQuestion 2] => 1 )
)
I need to get it into this format for Highcharts:
$chartdata = array(
array("name" =>"SubQuestion 1","data"=> array(3,2,1,1,0) ),
array("name" =>"SubQuestion 2","data"=> array(1,2,1,2,1) )
);
Can anyone please point me in the right direction as to how to iterate through my array to create a new array in the HighCharts format?
Thanks for all the help. I just wanted to close the loop and post the slightly modified version of the correct answer:
$chartdata = array();
foreach($series as $key1 => $value1){
$i=0;
foreach($value1 as $key2 => $value2){
$chartdata[$i][‘name’] = $key2;
$chartdata[$i][‘data’][] = $value2;
$i++;
}
}
1 Answer