doujing1967 2016-03-08 08:46
浏览 13
已采纳

多维数组和值

I have following function to sum multidimensional array values.

// summing values of multidimensional array
function getSum($array, $path = array()){
    // process second argument:
    foreach ($path as $key) {
        if (!is_array($array) || !isset($array[$key])) {
            return 0; // key does not exist, return 0
        }
        $array = $array[$key];
    }
    if(is_array($array)) {
        $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
        $sum = 0;
        foreach ($iterator as $key => $value) {
            $sum += $value;
        }
    } else{
        $sum = $array;
    }
    return $sum;
}

I'm using the function like this:

$array = array();
$array['one']['green'][20] = 20;
$array['one']['blue'][20] = 5;
$array['one']['blue'][30] = 10;

getSum($array,['one','green']); // 20
getSum($array,['one','blue',20]); // 5

Now I have a problem if I don't want to for example set any spesific color because I want that script sums all values from category 20 from all colours.

So it should be working like this:

getSum($array,['one','*',20]); // 25

Thanks for your help!

Here is example of my array:

Array ( 
    [1] => Array ( 
        [AREA I] => Array (
            [20] => 1 
            [25] => 0 
            [30] => 0 ) 
        [AREA II] => Array ( 
            [20] => 0 
            [30] => 0 ) 
        [AREA III] => Array ( 
            [20] => 2 
            [30] => 0 ) 
        [AREA IV] => Array ( 
            [20] => 0 
            [30] => 3 ) 
        [AREA V] => Array ( 
            [20] => 4 
            [25] => 0 
            [30] => 3 )  
    ) 
    [2] => Array ( 
        [AREA I] => Array ( 
            [20] => 0 
            [30] => 0 ) 
        [AREA II] => Array ( 
            [20] => 0 
            [30] => 0 ) 
    )
)

And here is example of my getSum call:

getSum($visitsandinfosact,['*','*',20]); // should print 7
  • 写回答

2条回答 默认 最新

  • duanpanbo9476 2016-03-08 12:08
    关注

    Recursive Function

    I was not sure if ['one','*'] should give 45 but if it should just return 0 you just have to remove the else if (empty($filterList) && is_array($value) && $first == "*")condition. All values which are not arrays are just converted to int via intval and added to the sum. If you wanna use float then use floatval instead of intval

    function getSum($array, $filterList = array('*')) {
        $sum = 0;
        $first = array_shift($filterList);
        foreach ($array as $key => $value) {
            if ($key == $first || $first == "*") {
                if (is_array($value) && !empty($filterList)) {
                    $sum += getSum($value, $filterList);
                } else if (empty($filterList) && is_array($value)) {
                    $sum += getSum($value, array("*"));
                } else if (empty($filterList)) {
                    $sum += intval($value);
                }
            }
        }
        return $sum;
    }
    
    echo getSum($array,['one','*',20], 10) . "
    "; // 25
    echo getSum($array,['one','*','*',20]) . "
    "; // 10
    echo getSum($array,['one','*']) . "
    "; // 45
    echo getSum($array) . "
    "; // 45
    

    Input Array

    $array = array();
    $array['one'] = array();
    $array['one']['green'] = array();
    $array['one']['green'][20] = 20;
    $array['one']['blue'] = array();
    $array['one']['blue'][20] = 5;
    $array['one']['blue'][30] = 10;
    $array['one']['orange']['red'][20] = 10;
    

    Output

    Only the numbers are outputted but just added the input params for better understanding.

    25 // (['one','*',20])
    10 // (['one','*','*',20])
    45 // (['one','*'])
    45 // no filterList
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里