通过索引键聚合多维数组的优雅方法

发布于 2024-08-31 06:49:15 字数 970 浏览 0 评论 0原文

如何递归地找到看起来像这样的数组的所有子项的总价值?

 [0] => Array
    (
        [value] => ? // 8590.25 + 200.5 + 22.4
        [children] => Array
            (
                [0] => Array
                    (
                        [value] => ? // 8590.25 + 200.5
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [value] => 8590.25 // leaf node
                                    )
                                [1] => Array
                                    (
                                        [value] => 200.05 // leaf node
                                    )
                            )

                    )
                [1] => Array
                    (
                        [value] => 22.4 // leaf node
                    )
             )
    )

How can I recursively find the total value of all children of an array that looks something like this?

 [0] => Array
    (
        [value] => ? // 8590.25 + 200.5 + 22.4
        [children] => Array
            (
                [0] => Array
                    (
                        [value] => ? // 8590.25 + 200.5
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [value] => 8590.25 // leaf node
                                    )
                                [1] => Array
                                    (
                                        [value] => 200.05 // leaf node
                                    )
                            )

                    )
                [1] => Array
                    (
                        [value] => 22.4 // leaf node
                    )
             )
    )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

身边 2024-09-07 06:49:15

在这种情况下,我会使用类而不是数组。这样,您就可以使用 getValue () 方法(或使用 magic 来使用 __get 定义 value 属性),根据需要对子值求和。如果您保证事情在某个点之后不会发生变化,则可以缓存这些子总和以避免重复计算。也许是这样的?

class DataStructure
{
  private $children = array ();
  private $value = 0;

  public function __construct ($value = 0)
  {
    $this->value = $value;
  }

  public function getValue ()
  {
    $total = $this->value;
    foreach ($this->children as $child)
    {
      $total += $child->getValue ();
    }
    return $total;
  }

  public function addChild (DataStructure $child)
  {
    $this->children[] = $child;
  }
}

This is the kind of case where I'd use a class instead of an array. That way, you can have a getValue () method (or use magic to define the value property using __get), that sums the child values on demand. If you have guarantees that things won't change after a point, you can cache those child sums to avoid repetetive computations. Maybe something like this?

class DataStructure
{
  private $children = array ();
  private $value = 0;

  public function __construct ($value = 0)
  {
    $this->value = $value;
  }

  public function getValue ()
  {
    $total = $this->value;
    foreach ($this->children as $child)
    {
      $total += $child->getValue ();
    }
    return $total;
  }

  public function addChild (DataStructure $child)
  {
    $this->children[] = $child;
  }
}
爱你是孤单的心事 2024-09-07 06:49:15

这将为您提供叶节点值的总和:

$sum = 0;
array_walk_recursive($arr, create_function('$v, $k, $sum', '$sum[0] += $v;'), array(&$sum));

相当于使用匿名函数(PHP 5.3+):

$sum = 0;
array_walk_recursive($arr, function ($v) use (&$sum) { $sum += $v; });

This will give you the total sum of the leaf node values:

$sum = 0;
array_walk_recursive($arr, create_function('$v, $k, $sum', '$sum[0] += $v;'), array(&$sum));

Equivalent using anonymous functions (PHP 5.3+):

$sum = 0;
array_walk_recursive($arr, function ($v) use (&$sum) { $sum += $v; });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文