首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何计算多维数组的值?

问如何计算多维数组的值?
EN

Stack Overflow用户
提问于 2012-06-25 03:41:58
回答 2查看 3.9K关注 0票数 3

我正在用PHP构建一个传销软件,它的功能之一是统计下线性能。它从父子关系创建一个数组,如下所示。我如何获得我的孩子和他们的孙子的表现(数组键:点),以及通过第五个向下的级别?

代码语言:javascript
复制
    Array
(
    [children] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [name] => Medvedev
                    [email] => 
                    [points] => 7
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 3
                                    [name] => Putin
                                    [email] => 
                                    [points] => 4
                                    [children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [id] => 5
                                                    [name] => Nathan
                                                    [email] => 
                                                    [points] => 3
                                                )

                                            [1] => Array
                                                (
                                                    [id] => 7
                                                    [name] => George
                                                    [email] => 
                                                    [points] => 666
                                                )

                                        )

                                )

                            [1] => Array
                                (
                                    [id] => 4
                                    [name] => Lucas
                                    [email] => 
                                    [points] => 43
                                )

                        )

                )
        )

    [id] => 27
    [name] => Boss
    [email] => 
    [points] => 99999
)
EN

回答 2

Stack Overflow用户

发布于 2012-06-25 05:18:11

这应该适用于从主数组开始的无限深度,比如

代码语言:javascript
复制
$array = array(
    'children' => array( /* ADD HERE INFINITE COMBINATION OF CHILDREN ARRAY */ ),
    'id' => #,
    'name' => '',
    'email' => '',
    'points' => #
);

function recursive_children_points($arr) {
    global $hold_points;
    if (isset($arr['points'])) {
        $hold_points[] = $arr['points'];
    }
    if (isset($arr['children'])) {
        foreach ($arr['children'] as $children => $child) {
            recursive_children_points($child);
        }
    }
    return $hold_points;
}

$points = recursive_children_points($array);
print "<pre>";
print_r($points);
/**
     // OUTPUT
     Array
(
    [0] => 99999
    [1] => 7
    [2] => 4
    [3] => 3
    [4] => 666
    [5] => 43
)
**/
    print "<pre>";
票数 3
EN

Stack Overflow用户

发布于 2012-06-25 05:24:53

在我看来,这需要使用recursion,因为您将在数组的每个平面上执行相同的操作:添加点。

所以你需要

  • 循环遍历每个数组
  • 如果我们发现任何子数组,请再次执行此操作,但使用子数组

同时跟踪关卡并在达到极限时跳出

考虑到这一点,我想到了以下解决方案:

代码语言:javascript
复制
<?php

$values = array();

//first
$values[] = array('name'=>'andrej','points'=>1,'children'=>array());
$values[] = array('name'=>'peter','points'=>2,'children'=>array());
$values[] = array('name'=>'mark','points'=>3,'children'=>array());

//second
$values[0]['children'][] = array('name'=>'Sarah','points'=>4,'children'=>array());
$values[2]['children'][] = array('name'=>'Mike','points'=>5,'children'=>array());

//third 
$values[0]['children'][0]['children'][] = array('name'=>'Ron','points'=>6,'children'=>array());

//fourth
$values[0]['children'][0]['children'][0]['children'][] = array('name'=>'Ronny','points'=>7,'children'=>array());

//fifth
$values[0]['children'][0]['children'][0]['children'][0]['children'][] = array('name'=>'Marina','points'=>10,'children'=>array());

//sixth
$values[0]['children'][0]['children'][0]['children'][0]['children'][0]['children'][] = array('name'=>'Pjotr','points'=>400,'children'=>array());


function collect_elements($base, $maxLevel,$child='children',$gather='points', &$catch = array(), $level = 0) {
/*    I pass $catch by reference so that all recursive calls add to the same array
      obviously you could use it straight away but I return the created array as well
  because I find it to be cleaner in PHP (by reference is rare and can lead to confusion)

  $base = array it works on
  $maxLevel = how deep the recursion goes

  $child = key of the element where you hold your possible childnodes
      $gather = key of the element that has to be collected
*/

  $level++;
  if($level > $maxLevel) return; // we're too deep, bail out

  foreach ($base as $key => $elem) {
    // collect the element if available
    if(isset($elem[$gather])) $catch[] = $elem[$gather];

    /*
    does this element's container have children? 
       [$child] needs to be set, [$child] needs to be an array, [$child] needs to have elements itself
    */
    if (isset($elem[$child]) && is_array($elem[$child]) && count($elem[$child])){
       // if we can find another array 1 level down, recurse that as well  
       collect_elements($elem[$child],$maxLevel,$child,$gather, $catch,$level); 
    }
  }
return $catch;
}

print array_sum(collect_elements($values,5)) . PHP_EOL;

collect_elements将收集您感兴趣的元素(直到达到最大深度),并将其附加到平面数组中,以便您可以在返回时对其执行操作。在本例中,我们执行array_sum以获取所收集的点的总数

只有第一个for参数比较有趣:

代码语言:javascript
复制
collect_elements($base, $maxLevel,$child='children',$gather='points'

非可选:$base是要处理的数组$maxLevel是函数下降到数组所需的最大深度可选:$child定义包含当前元素(数组)的子元素的元素的键$gather定义包含我们要收集的内容的元素的键

其余的参数只是用于递归的参数

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11180646

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档