Php, calculate percentage on multiple items

后端 未结 5 1370
迷失自我
迷失自我 2020-12-20 05:31

I have 5 items in total, and I would like to calculate percentage based on [data] filed. The result will be used for pie chart.

Array
(
    [0] => Array
          


        
相关标签:
5条回答
  • 2020-12-20 06:04

    No loop needed. array_column & array_sum will help. You can try this -

    $a= array(
    array('label'=>"Item1",'data'=>849),
    array('label'=>"Item2",'data'=>657),
    array('label'=>"Item3",'data'=>571),
    array('label'=>"Item4",'data'=>538),
    array('label'=>"Item5",'data'=>446)
    );
    
    echo "Percentage : " . ((5 / (array_sum(array_column($a, 'data')))) * 100);
    

    Output

    Percentage : 0.16334531198955
    
    0 讨论(0)
  • 2020-12-20 06:08

    I think what you want is to sum up all the items to get the total sum first and then determine the percentage of each item

    $sum = array[0]['data'] + array[1]['data'] ...
    $pc0 = array[0]['data'] / $sum * 100;
    ...
    

    I leave the looping to the OP.

    // EDIT: As for a lack of something better to do, here is a reduce function to get the sum:

    $sum = array_reduce($data_array, function($v1, $v2){ return $v1 + $v2['data']; });
    
    0 讨论(0)
  • 2020-12-20 06:10
    <?php 
    $array=array(0=>array('label'=>"Item1",'data'=>849),
    1=>array('label'=>"Item2",'data'=>657),
    2=>array('label'=>"Item3",'data'=>571),
    3=>array('label'=>"Item4",'data'=>538),
    4=>array('label'=>"Item5",'data'=>446)
    );$val=0;
    foreach($array as $value){
    //print_r($value['data']);
    $val+=$value['data'];
    }
    echo "output  =  ".(5/$val)*100;
    ?>
    
    0 讨论(0)
  • 2020-12-20 06:13

    Simple With loop method to do

    $arr = array
    (
    0 => array
        (
            "label" => "Item1",
            "data" => 849,
        ),
    
    1 => array
        (
            "label" => "Item2",
            "data" => 657,
        ),
    
    2 => array
        (
            "label" => "Item3",
            "data" => 571,
        ),
    
    3 => array
        (
            "label" => "Item4",
            "data" => 538,
        ),
    
    4 => array
        (
            "label" => "Item5",
            "data" => 446,
        ),
    
    );
    
    $totalElement = count($arr);
    $data = 0;
    foreach ($arr as $key => $value) {
        $data += $value['data'];
    }
    
    echo ($totalElement/$data)*100;
    

    result

     0.16334531198955
    
    0 讨论(0)
  • 2020-12-20 06:20

    If you want your code to be flexible:

    $array= [
          0 => [
            'label' => 'Item1',
            'data' => 849,
            ],
          1 => [
            'label' => 'Item1',
            'data' => 849,
            ],
          2 => [
            'label' => 'Item1',
            'data' => 849,
            ],
          3 => [
            'label' => 'Item1',
            'data' => 849,
          ],
          4 => [
            'label' => 'Item1',
            'data' => 849,
            ],
          5 => [
            'label' => 'Item1',
            'data' => 849,
            ]
    ];
       foreach($array as $key => $val){
         $sum +=$val['data'];
       }
      echo "output  =  ".(count($val['data'])/$sum)*100;
    ?>
    
    0 讨论(0)
提交回复
热议问题