php - how to prepare data format to show in c3js chart? -


i want show percent of department depend on date in c3js chart timeseries.

i have 4 department id, , query result this.

$dept_id_arr = array(); $date_arr = array(); $percent_arr =  array(); $sql = ........; $rsl = mysql_query($sql); while($get = mysql_fetch_assoc($rsl)){     $date_arr[] = $get['date'];     $dept_id_arr[] = $get['department_id'];     $percent_arr[] = $get['total_percent']; } 

when print data var_dump(), got this,

string(66) "["2015-11-17","2015-11-17","2015-11-18","2015-11-20","2015-11-23"]"  string(22) "["1","3","1","1","2"]"  string(46) "["0.5700","0.0000","0.5700","0.0000","0.5700"]"  

i want change value this,

[{         "date": "2015-11-17",         "department1": "0.5",         "department2": "0.9",         "department3": "4",         "department4": "3",     }, {         "date": "2015-11-18",         "department1": "0.5",         "department2": "0",         "department3": "0",         "department4": "0",     }, {         "date": "2015-11-19",         "department1": "0.5",         "department2": "0.3",         "department3": "5",         "department4": "2",     }] 

because need show data in c3js chart. have problem, when change data format because of date same in array.

you can things code:

<?php $date = ["2015-11-17","2015-11-17","2015-11-18","2015-11-20","2015-11-23"]; $departmeent = ["1","3","1","1","2"]; $percentage = ["0.5700","0.0000","0.5700","0.0000","0.5700"]; $array = array();  for($i=0; $i<5;$i++) {     if(array_key_exists($date[$i], $array))     {         $array[$date[$i]]['department'.$departmeent[$i]]= $departmeent[$i];         $array[$date[$i]]['per'.$departmeent[$i]]= $percentage[$i];      }     else     {          $array[$date[$i]] = array('department'.$departmeent[$i] => $departmeent[$i],                     'per'.$departmeent[$i] => $percentage[$i]) ;     }     }  echo var_dump($array["2015-11-17"]["department1"]);  echo '<br><br><br><br><br><br>'.json_encode($array); ?> 

Comments