我正在尝试为浮点饼图获取正确格式的数据。我已经设法获得了正确的数据来输出,但我需要帮助才能将其转换为正确的格式,以便饼图能够正常工作。
Array
(
[0] => Array
(
[incident_type] => intoxicated_persons, street_offenses,
)
[1] => Array
(
[incident_type] => intoxicated_persons,
)
[2] => Array
(
[incident_type] => information_only,
)
[3] => Array
(
[incident_type] => information_only,
)
[4] => Array
(
[incident_type] => information_only,
)
[5] => Array
(
[incident_type] => information_only,
)
[6] => Array
(
[incident_type] => brawl,
)
[7] => Array
(
[incident_type] => intoxicated_persons,
)
[8] => Array
(
[incident_type] => information_only,
)
[9] => Array
(
[incident_type] => information_only,
)
[10] => Array
(
[incident_type] => brawl,
)
[11] => Array
(
[incident_type] => information_only,
)
[12] => Array
(
[incident_type] => information_only,
)
[13] => Array
(
[incident_type] => intoxicated_persons,
)
[14] => Array
(
[incident_type] => brawl,
)
[15] => Array
(
[incident_type] => brawl,
)
[16] => Array
(
[incident_type] => stealing,
)
[17] => Array
(
[incident_type] => information_only,
)
[18] => Array
(
[incident_type] => information_only,
)
[19] => Array
(
[incident_type] => intoxicated_persons, street_offenses,
)
[20] => Array
(
[incident_type] => information_only,
)
上面的数据相同,下面是我的函数:
public function show_pie_data()
{
//Use the same data
//$all_incident_type = $this->incidents_m->get_all_incident_types();
$result = array();
$final_result = array();
foreach($all_incident_type as $value)
{
$date_bits = explode(', ', $value['incident_type']);
foreach ($date_bits as $key => $value) {
if($value != ''){
$result[] = $value;
}
}
}
//print_r(array_count_values($result));
foreach (array_count_values($result) as $k => $v) {
$final_result[] = "{label:". $k .", data:". $v ."}";
}
// {label:"test",data:10}
return json_encode($final_result);
}