PHP中的树阵列父节点更新

时间:2018-03-22 11:53:24

标签: php recursion

我有一个这样的数组:

Array(
[id] => test1
[data] => Array([balance] => 0 )
[has_children] => 1
[children] => Array(
        [0] => Array
            ([id] => test2
            [data] => Array([balance] => 45)
            [has_children] => 
            )
        [1] => Array(
                [id] => test3
                [data] => Array([balance] => 78)
                [has_children] => 
            )
        [2] => Array(
                [id] => test55
                [data] => Array([balance] => 0)
                [has_children] => 1
                [children] => Array(
                        [0] => Array(
                                [id] => test45
                                [data] => Array([balance] => 90)
                                [has_children] => 
                            )
                    )
            )
        [3] => Array(
                [id] => test567
                [data] => Array([balance] => 123)
                [has_children] => 
            )
        [4] => Array(
                [id] => test79
                [data] => Array([balance] => 34)
                [has_children] => 
            )
    )
)

我想将父余额更新为SUM(子节点的余额)。如何更新这个。 父节点余额为(SUM(子节点的余额)。

这是我的代码:

foreach ($array as &$each) {
    $balance = 0;
    $this->getBalance($each,$balance);
    $array['data']['balance'] = $balance;
    print_r($array);
    exit;
}
public function getBalance($data, &$balance) {
    $balance += $data['data']['balance'];
    if (isset($data['has_children']) && $data['has_children']) {
        if (isset($data['children']) && !empty($data['children'])) {
            foreach ($data['children'] as &$eachChildren) {
                $this->getBalance($eachChildren, $balance);
            }

        }
    }
}

在此代码中,父节点余额仅添加,更新。如何更新所有父节点余额???

2 个答案:

答案 0 :(得分:0)

foreach($array["children"] as $key=>$value){
  $sum = 0;
  if($value["has_children"] != ""){
  foreach($value["children"] as $k1=>$v1){
    $sum += (int)$v1["data"][0]["balance"];
  }
  $value["data"][0]["balance"] = $sum;
 }
}

在循环中使用值0初始化$sum。循环遍历外部数组并检查每个密钥是否为has_children?如果是,则再循环通过内部数组并将余额添加到$ sum变量。在内循环结束时,你将拥有所有孩子的总余额。现在将此总数分配给外部数组的余额。

更新

对于无限的孩子​​

foreach($array["children"] as $key=>$value){
      if($value["has_children"] != ""){
      $value["data"][0]["balance"] = get_sum($value["children"]);
     }
    }

   function get_sum($child_array){
   $innersum = 0;
   foreach($child_array as $k=>$v){
     if($v["has_children"] == ""){
      $innersum += $v["data"][0]["balance"];
     }else{
      get_sum($v["children"]);
     }
   }
   return $innersum;
   }

答案 1 :(得分:0)

这是正确的代码:

public function assignBalances(&$balanceData)
    {
        foreach ($balanceData as &$each)
        {
            $balance = 0;
            $this->getBalance($each, $balance);
            $each['data']['balance'] = $balance;
            if ($each['has_children'])
            {
                $level++;
                $this->assignBalances($each['children']);
            }
        }

    }

    public function getBalance($data, &$balance)
    {
        $balance += (int) ($data['data']['balance']);
        if (isset($data['has_children']) && $data['has_children'])
        {
            if (isset($data['children']) && !empty($data['children']))
            {
                foreach ($data['children'] as &$eachChildren)
                {
                    $this->getBalance($eachChildren, $balance);
                }
            }
        }
    }

它工作得很完美..