3

私はPHPでMLMソフトウェアを構築していますが、その機能の1つは、ダウンラインのパフォーマンスをカウントすることです。親子関係から配列を作成します。これを以下に示します。5番目のレベルまで、子供とその孫のパフォーマンス(配列キー:ポイント)を取得するにはどうすればよいですか?

    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
)
4

2 に答える 2

3

これは、次のようなメイン配列から始まる無制限の深さで機能するはずです

$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>";
于 2012-06-24T21:18:10.570 に答える
1

私の意見では、配列の各フラット レベルで同じことを行うため、これには再帰が必要です。つまり、ポイントを追加します。

だからあなたがする必要があります

  • 各配列をループする
  • 見つけたポイントを追加
  • 子が見つかったら、もう一度やり直しますが、子配列を使用します

レベルを追跡しながら、限界に達したら飛び出します

それを念頭に置いて、次の解決策を考えました。

<?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 パラメータのみです。

collect_elements($base, $maxLevel,$child='children',$gather='points'

オプションではありません: $base処理する配列は $maxLevel、関数が配列に降りるのに必要な最大の深さです オプション: $child現在の要素 (配列) の子を含む要素のキーを定義し ます 必要$gatherなものを含む要素のキーを定義します集める

残りのパラメータは、再帰に使用されるものです

于 2012-06-24T21:24:52.853 に答える