如何使递归函数计算多维数组中的平均值

时间:2017-11-24 03:47:18

标签: php arrays recursion

如何通过以下多维数组循环递归,但仍然按标准计算平均值?这是我的初始数组,然后查找我的代码(循环播放)每个级别都不理想)然后输出。

我正在寻找可以减少或改进此代码的功能,以便我可以重新利用来计算其他级别的平均值。

Array
(
    [42715] => Array
        (
            [1886] => Array
                (
                    [1252] => Array
                        (
                            [1016] => Array
                                (
                                    [Standard 1] => Array
                                        (
                                            [0] => 1.0000000
                                            [1] => 0.5000000
                                            [2] => 1.0000000
                                            [3] => 1.0000000
                                            [4] => 1.0000000
                                            [5] => 1.0000000
                                        )

                                    [1] => 10/24/2017
                                    [Standard 2] => Array
                                        (
                                            [0] => 1.0000000
                                            [1] => 1.0000000
                                            [2] => 1.0000000
                                        )

                                    [Standard 3] => Array
                                        (
                                            [0] => 0.0000000
                                            [1] => 0.0000000
                                            [2] => 0.0000000
                                        )

                                    [Standard 4] => Array
                                        (
                                            [0] => 1.0000000
                                            [1] => 1.0000000
                                            [2] => 1.0000000
                                        )

                                    [Standard 5] => Array
                                        (
                                            [0] => 1.0000000
                                            [1] => 1.0000000
                                            [2] => 0.0000000
                                        )

                                    [Standard 6] => Array
                                        (
                                            [0] => 0.0000000
                                            [1] => 1.0000000
                                            [2] => 1.0000000
                                            [3] => 0.0000000
                                            [4] => 1.0000000
                                            [5] => 1.0000000
                                        )

                                    [Standard 7] => Array
                                        (
                                            [0] => 1.0000000
                                            [1] => 1.0000000
                                            [2] => 1.0000000
                                        )

                                    [Standard 8] => Array
                                        (
                                            [0] => 0.0000000
                                            [1] => 1.0000000
                                            [2] => 0.0000000
                                        )

                                    [Standard 9] => Array
                                        (
                                            [0] => 0.0000000
                                            [1] => 0.0000000
                                            [2] => 0.0000000
                                        )

                                )

                            [1019] => Array
                                (
                                    [Standard 8] => Array
                                        (
                                            [0] => 1.0000000
                                            [1] => 0.0000000
                                            [2] => 0.0000000
                                            [3] => 1.0000000
                                            [4] => 0.0000000
                                            [5] => 0.0000000
                                            [6] => 0.0000000
                                        )

                                    [1] => 10/24/2017
                                )

                            [1017] => Array
                                (
                                    [Standard 1] => Array
                                        (
                                            [0] => 0.0000000
                                        )

                                    [1] => 10/24/2017
                                    [Standard 2] => Array
                                        (
                                            [0] => 1.0000000
                                            [1] => 0.0000000
                                        )

                                    [Standard 3] => Array
                                        (
                                            [0] => 0.0000000
                                            [1] => 0.0000000
                                        )

                                    [Standard 4] => Array
                                        (
                                            [0] => 1.0000000
                                        )

                                    [Standard 5] => Array
                                        (
                                            [0] => 0.0000000
                                        )

                                    [Standard 6] => Array
                                        (
                                            [0] => 0.0000000
                                        )

                                    [Standard 7] => Array
                                        (
                                            [0] => 1.0000000
                                        )

                                    [Standard 8] => Array
                                        (
                                            [0] => 0.0000000
                                        )

                                    [Standard 9] => Array
                                        (
                                            [0] => 1.0000000
                                        )

                                )

                        )

                )

我使用以下代码计算9种标准品的平均值。

$quizrow = array();
foreach ($modulet as $teacherid => $teacherlist) {
    foreach ($teacherlist as $classid => $classlist) {
        foreach ($classlist as $courseid => $quizlist) {
            foreach ($quizlist as $quizid => $stdlist) {

                $count = $total = 0;
                $timemodified   = $stdlist['timemodified'];

                // Remove this element so we only have Standards.
                unset($stdlist['timemodified']);

                // Reorder the array so we get Standard 1, Standard 2, etc...
                ksort($stdlist, 2);
                foreach ($stdlist as $name => $std) {
                    $quizrow[$teacherid][$classid][$courseid][$quizid][0] = btr_get_quiz_name($quizid);
                    $quizrow[$teacherid][$classid][$courseid][$quizid][1] = $timemodified;
                    $quizrow[$teacherid][$classid][$courseid][$quizid][2] = "";

                    // Calculate the average of the Standards.
                    $average = round(array_sum($std) / count($std) * 100, 1) . "%";
                    $quizrow[$teacherid][$classid][$courseid][$quizid][$name] = $average;
                    $total += $average;
                    $count++;
                }

                // The second index will hold the average results for all Standards.
                $quizrow[$teacherid][$classid][$courseid][$quizid][2] = round($total / $count, 1) . "%";
            }
        }
    }
}

输出:

Array
(
    [42715] => Array
        (
            [1886] => Array
                (
                    [1252] => Array
                    (
                        [1016] => Array
                            (
                                [0] => Name
                                [1] => 10/24/2017
                                [2] => 62%
                                [Standard 1] => 91.7%
                                [Standard 2] => 100%
                                [Standard 3] => 0%
                                [Standard 4] => 100%
                                [Standard 5] => 66.7%
                                [Standard 6] => 66.7%
                                [Standard 7] => 100%
                                [Standard 8] => 33.3%
                                [Standard 9] => 0%
                            )

                        [1019] => Array
                            (
                                [0] => Name
                                [1] => 10/24/2017
                                [2] => 58.7%
                                [Standard 8] => 28.6%
                            )

                        [1017] => Array
                            (
                                [0] => Name
                                [1] => 10/24/2017
                                [2] => 49.3%
                                [Standard 1] => 0%
                                [Standard 2] => 50%
                                [Standard 3] => 0%
                                [Standard 4] => 100%
                                [Standard 5] => 0%
                                [Standard 6] => 0%
                                [Standard 7] => 100%
                                [Standard 8] => 0%
                                [Standard 9] => 100%
                            )

                    )

            )

编辑更新:再次组织输出数组,以计算下一级别的平均值。

Array
(
    [42715] => Array
        (
            [1886] => Array
                (
                    [1252] => Array
                        (
                            [0] => Array
                                (
                                    [0] => Name
                                    [1] => Name
                                    [2] => Name
                                )

                            [1] => Array
                                (
                                    [0] => 10/24/2017
                                    [1] => 10/24/2017
                                    [2] => 10/24/2017
                                )

                            [2] => Array
                                (
                                    [0] => 62%
                                    [1] => 58.7%
                                    [2] => 49.3%
                                )

                            [Standard 1] => Array
                                (
                                    [0] => 91.7%
                                    [1] => 0%
                                )

                            [Standard 2] => Array
                                (
                                    [0] => 100%
                                    [1] => 50%
                                )

                            [Standard 3] => Array
                                (
                                    [0] => 0%
                                    [1] => 0%
                                )

                            [Standard 4] => Array
                                (
                                    [0] => 100%
                                    [1] => 100%
                                )

                            [Standard 5] => Array
                                (
                                    [0] => 66.7%
                                    [1] => 0%
                                )

                            [Standard 6] => Array
                                (
                                    [0] => 66.7%
                                    [1] => 0%
                                )

                            [Standard 7] => Array
                                (
                                    [0] => 100%
                                    [1] => 100%
                                )

                            [Standard 8] => Array
                                (
                                    [0] => 33.3%
                                    [1] => 28.6%
                                    [2] => 0%
                                )

                            [Standard 9] => Array
                                (
                                    [0] => 0%
                                    [1] => 100%
                                )

                        )

                )

我再次使用foreach循环到最深层,使用非常相似的代码获得平均输出:

Array
(
    [42715] => Array
        (
            [1886] => Array
                (
                    [1252] => Array
                        (
                            [0] => Unit of Study: The Price of Fashion – 830L
                            [1] => 10/24/2017
                            [2] => 50.9%
                            [Standard 1] => 45.9%
                            [Standard 2] => 75%
                            [Standard 3] => 0%
                            [Standard 4] => 100%
                            [Standard 5] => 33.4%
                            [Standard 6] => 33.4%
                            [Standard 7] => 100%
                            [Standard 8] => 20.6%
                            [Standard 9] => 50%
                        )

                )

我必须这样做,直到达到最新水平。

2 个答案:

答案 0 :(得分:1)

[见过程说明的内联评论]

代码:(Demo

function recursive_calculator($array){
    foreach($array as $id=>&$elem){  // make every level modifiable
        if(!isset($elem['timemodified'])){
            $elem=recursive_calculator($elem);  // I am using timemodified to indicate whether there are scores to process
        }else{
            $elem[0]='Quiz Name'; // btr_get_quiz_name($id);
            $elem[1]=$elem['timemodified'];  // store date with a new key
            unset($elem['timemodified']);    // remove old element
            $score_sets=array_filter($elem,function($k){return strpos($k,'Standard ')===0;},ARRAY_FILTER_USE_KEY);  // isolate quiz score subarrays only
            foreach($score_sets as $name=>$scores){
                $elem[$name]=($averages[]=round(array_sum($scores)/count($scores)*100,1))."%"; // double declaration, $averages will be used outside of the loop
            }
            $elem[2]=round(array_sum($averages)/count($averages),1)."%";  // calculate the cumulative average for all sets/quizzes
            ksort($elem,SORT_NATURAL);  // sort the data as desired
        }
    }
    return $array;  // return the full modified data structure
}

$modulet=[
    42715=>[
        1886=>[
            1295=>[
                1166=>[
                    'Standard 1'=>[0.5000000,0.5000000,1.0000000,0.5000000,0.5000000,0.5000000,0.5000000,0.5000000,0.5000000,0.5000000,0.5000000],
                    'timemodified'=>'10/23/2017',
                    'Standard 2'=>[0.0000000,1.0000000,1.0000000,1.0000000,0.0000000,0.0000000,0.0000000,0.0000000,1.0000000,0.0000000,0.0000000]
                ]
            ]
        ]
    ]
];

var_export(recursive_calculator($modulet));

输出:

array (
  42715 => 
  array (
    1886 => 
    array (
      1295 => 
      array (
        1166 => 
        array (
          0 => 'Quiz Name',
          1 => '10/23/2017',
          2 => '45.5%',
          'Standard 1' => '54.5%',
          'Standard 2' => '36.4%',
        ),
      ),
    ),
  ),
)

答案 1 :(得分:0)

如果你只需要整个数组的值,而不关心索引,你可以这样做:

 <?php                                                                      
    $a = array(array(array(array(array(1,2,3,4,5,99)))));                      
    $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($a));       
    $s = array();                                                              
    foreach($it as $v) {                                                       
      $s[] = $v;                                                               
    }                                                                          
    echo(array_sum($s)/count($s));                                             

请参阅: How to Flatten a Multidimensional Array?