PHP饼图数据格式

时间:2013-06-22 08:36:47

标签: php arrays codeigniter

我正在尝试以正确的格式获取flot饼图的数据。我已设法获得正确的数据输出,但我需要帮助将其设置为正确的格式,以便饼图可以工作。

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);
}

1 个答案:

答案 0 :(得分:0)

这是我的解决方案:

<?php

/**
 * @author lolkittens
 * @copyright 2013
 */

$cases = array(
    array(
        'incident_type' => 'brawl'
    ),
    array(
        'incident_type' => 'brawl'
    ),
    array(
        'incident_type' => 'brawl'
    ),
    array(
        'incident_type' => 'brawl'
    ),
    array(
        'incident_type' => 'brawl'
    ),
    array(
        'incident_type' => 'intoxicated_persons'
    ),
    array(
        'incident_type' => 'intoxicated_persons'
    ),
    array(
        'incident_type' => 'intoxicated_persons'
    ),
    array(
        'incident_type' => 'intoxicated_persons'
    ),
    array(
        'incident_type' => 'intoxicated_persons'
    ),
);
$flat = array();
$unique = array();
$counts = array();
$final = array();
$json = array();
//FILTER THE UNIQUE VALUES
foreach($cases as $key => $value){

    foreach($value as $k => $v){

        if(!in_array($v,$unique)){
            $unique[] = $v;
        }
    }
}
// FLATTEN THE ARRAY
foreach($cases as $key => $value){

    foreach($value as $k => $v){
        $flat[$key] = $v;
    }
}
// COUNT
foreach($flat as $key => $val){

    if(in_array($val,$unique)){
        if(array_key_exists($val,$final)){
            $final[$val] = $final[$val] + 1;
        }else{
            $final[$val] = 0;
        }
    }
}
$count = 0;
foreach($final as $k => $v){
   $json[$count]['label'] = $k;
   $json[$count]['data'] = $v;
   $count++; 
}

echo json_encode($json);
?>

结果:

[{"label":"brawl","data":4},{"label":"intoxicated_persons","data":4}]