0

我正在尝试为浮点饼图获取正确格式的数据。我已经设法获得了正确的数据来输出,但我需要帮助才能将其转换为正确的格式,以便饼图能够正常工作。

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

1 回答 1

0

Here is my solution:

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

Result:

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

于 2013-06-22T11:38:11.250 回答