简体   繁体   English

递归搜索数组和求和字段

[英]Recursivly search array and sum field

I am trying to write a recursive function that will iterate over an array of arrays and sum a specific field. 我正在尝试编写一个递归函数,该函数将遍历数组数组并求和特定字段。 Here is an example of an array: 这是一个数组的示例:

{
  "68": {
    "10": [
      {
        "id": "3333",
        "sumTHis": "5"
      }
    ]
  },
  "69": {
    "45": [
      {
        "id": "3333",
        "sumTHis": "5"
      }
    ],
    "50": [
      {
        "id": "3330",
        "sumTHis": "5"
      },
      {
        "id": "3331",
        "sumTHis": "5"
      },
      {
        "id": "3332",
        "sumTHis": "5"
      },
      {
        "id": "3333",
        "sumTHis": "5"
      }
    ]
  }
}

The problem is that the array could be any number of sub-arrays deep. 问题在于该阵列可以是任意数量的子阵列。 In the end, I would like to be able to sum all "sumTHis" nodes throughout the entire array The code I have so far is this: 最后,我希望能够对整个数组中的所有“ sumTHis”节点求和。到目前为止,我的代码是:

//in body
$sumThis= recurse_get_total($array, 'sumTHis');


//recursive function
function recurse_get_total($report_data, $valId, $total = 0){
    try{
            foreach ($report_data as $key => $value) {
                if(is_array_of_arrays($value)){
                    recurse_get_total($value, $valId, $total);
                }else{
                   $total = $total + $value[$valId];
                   return $total;
                }
            }
        return $total;
    }catch(Exception $err){
        throw $err;
    }
}

function is_array_of_arrays($isArray){
    try{
        if(is_array($isArray)){
            foreach($isArray as $key => $value){
                if(!is_array($value)){
                    return false;
                }
            }
            return true;
        }
    }catch(Exception $err){
        throw $err;
    }
}

This function starts to iterate over the array but gets kicked out after the first one and returns 0. Can anyone help out? 此函数开始遍历数组,但在第一个数组后被踢出并返回0。有人可以帮忙吗?

Thanks jason 谢谢杰森

Going about this problem I set something up with "array_walk_recursive". 关于这个问题,我使用“ array_walk_recursive”进行了设置。 Seeing that you want to add some stuff independent of the depth of the arrays, this seems to work. 看到要添加一些与数组深度无关的东西,这似乎可行。 It is not solving it with what you have, but perhaps this different approach will get you there. 它不是用您所拥有的解决它,但是也许这种不同的方法将使您到达那里。

$sum = 0;

$array = array(
    "one" => array(
        "day" => "tuesday",
        "week" => "20",
        "findthis" => 10
    ),
    "two" => array("subone" => array(
            "some" => "one",
            "findthis" => 23
        )),
    "deeperthree" => array("subtwo" => array("deeper" => array(
                "one" => "entry",
                "findthis" => 44
            )))
);

function callback($val, $key, $arg) {

    if ($key == "findthis") {
        $arg[0]($val, $arg[1]);
    }

};

$function = function($num, &$sum) {
    $sum = $sum + $num;
    echo $sum . " ";
};

array_walk_recursive($array, "callback", array( $function, &$sum ));

result: 10 33 77 结果: 10 33 77

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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