Php,计算多个项目的百分比

时间:2015-08-12 04:23:26

标签: php percentage

我总共有5个项目,我想根据提交的[数据]计算百分比。结果将用于饼图。

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
        )

)

使用:

(5/[data])*100

没有产生正确的结果,我不知道如何进行正确的计算。

5 个答案:

答案 0 :(得分:2)

不需要循环。 array_column& array_sum会有所帮助。你可以试试这个 -

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

<强>输出

Percentage : 0.16334531198955

答案 1 :(得分:1)

我认为您想要的是总结所有项目以获得总和,然后确定每个项目的百分比

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

我将循环留给OP。

//编辑: 至于缺乏更好的事情,这里有一个减少函数来得到总和:

$sum = array_reduce($data_array, function($v1, $v2){ return $v1 + $v2['data']; });

答案 2 :(得分:0)

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

答案 3 :(得分:0)

如果您希望代码灵活:

#include <iostream>
#include <iomanip>

#include <type_traits>
#include <utility>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <deque>
#include <forward_list>
#include <vector>

#include <cstdlib>
#include <cassert>

template< typename pattern_iterator, typename target_iterator >
void
pattern_sort(pattern_iterator pbeg, pattern_iterator pend, target_iterator tbeg, target_iterator tend)
{
    //assert(std::distance(pbeg, pend) == std::distance(tbeg, tend));
    using pattern_traits = std::iterator_traits< pattern_iterator >;
    using target_traits = std::iterator_traits< target_iterator >;
    static_assert(std::is_base_of< std::forward_iterator_tag, typename pattern_traits::iterator_category >{});
    static_assert(std::is_base_of< std::forward_iterator_tag, typename target_traits::iterator_category >{});
    struct iterator_adaptor
    {

        iterator_adaptor(typename pattern_traits::reference pattern,
                         typename target_traits::reference target)
            : p(&pattern)
            , t(&target)
        { ; }

        iterator_adaptor(iterator_adaptor &&)
            : p(nullptr)
            , t(nullptr)
        { ; }

        void
        operator = (iterator_adaptor && rhs) &
        {
            if (!!rhs.p) {
                assert(!!rhs.t);
                std::swap(p, rhs.p);
                std::iter_swap(t, rhs.t);
            }
        }

        bool
        operator < (iterator_adaptor const & rhs) const
        {
            return (*p < *rhs.p);
        }

    private :

        typename pattern_traits::pointer p;
        typename target_traits::pointer t;

    };
    std::deque< iterator_adaptor > proxy_; // std::vector can be used instead
    //proxy_.reserve(static_cast< std::size_t >(std::distance(pbeg, pend))); // it's (maybe) worth it if proxy_ is std::vector and if walking through whole [tbeg, tend) range is not too expensive operation (in case if target_iterator is worse then RandomAccessIterator)
    auto t = tbeg;
    auto p = pbeg;
    while (p != pend) {
        assert(t != tend);
        proxy_.emplace_back(*p, *t);
        ++p;
        ++t;
    }
    std::sort(std::begin(proxy_), std::end(proxy_));
}

int
main()
{
    std::forward_list< int > keys{5, 4, 3, 2, 1};
    std::vector< std::size_t > indices(static_cast< std::size_t >(std::distance(std::cbegin(keys), std::cend(keys))));
    std::iota(std::begin(indices), std::end(indices), std::size_t{0}); // indices now: 0, 1, 2, 3, 4    
    std::copy(std::cbegin(keys), std::cend(keys), std::ostream_iterator< int >(std::cout, " ")); std::cout << std::endl;
    std::copy(std::cbegin(indices), std::cend(indices), std::ostream_iterator< std::size_t >(std::cout, " ")); std::cout << std::endl;
    pattern_sort(std::cbegin(keys), std::cend(keys), std::begin(indices), std::end(indices)); std::cout << std::endl;
    std::copy(std::cbegin(keys), std::cend(keys), std::ostream_iterator< int >(std::cout, " ")); std::cout << std::endl;
    std::copy(std::cbegin(indices), std::cend(indices), std::ostream_iterator< std::size_t >(std::cout, " ")); std::cout << std::endl;
    // now one can use indices to access keys and data to as ordered containers
    return EXIT_SUCCESS;
}

答案 4 :(得分:0)

使用loop方法进行简单

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

结果

 0.16334531198955