简体   繁体   中英

Php, calculate percentage on multiple items

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
        (
            [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
        )

)

Using:

(5/[data])*100

does not produce correct result, and I'm not sure how to perform proper calculations.

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

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']; });
<?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;
?>

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM