2016-09-02 73 views
0

這是所有數組專家的問題。我有一個多維數組,其結果爲數字(可以是0,1或2),並且需要每個按父級分組的平均值。PHP中多維數組的平均結果

在計算下面的例子將是:

subentry1_sub1 = 2 + 2 = 4(4/2 = )

subentry1_sub2 = 1 + 1 = 2(2/2 = )

所以我嘗試在PHP存檔如下結果:

subentry1_sub1平均= 2

subentry1_sub2平均= 1

...

我已經嘗試過相似的問題,一些解決方案。但是,所有的遞歸函數都沒有設法通過最後一個子名稱(例如subentry1_sub1)來聚合它。

任何想法?

編輯:

subentry1_sub1是2 + 2,因爲在陣列中的兩次

[取值範] => [subentry1] => [subentry1_sub1] =>導致

[ENTRY2 ] => [subentry1] => [subentry1_sub1] =>導致

Array 
(
    [entry1] => Array 
     (
      [subentry1] => Array 
       (
        [subentry1_sub1] => Array 
         (
          [value] => abc 
          [result] => 2 
         ) 

        [subentry1_sub2] => Array 
         (
          [value] => abc 
          [result] => 1 
         ) 

       ) 

      [subentry2] => Array 
       (
        [subentry2_sub1] => Array 
         (
          [value] => abc 
          [result] => 1 
         ) 

        [subentry2_sub2] => Array 
         (
          [value] => abc 
          [result] => 1 
         ) 

       ) 

     ) 

    [entry2] => Array 
     (
      [subentry1] => Array 
       (
        [subentry1_sub1] => Array 
         (
          [value] => abc 
          [result] => 2 
         ) 

        [subentry1_sub2] => Array 
         (
          [value] => abc 
          [result] => 1 
         ) 

       ) 

      [subentry2] => Array 
       (
        [subentry2_sub1] => Array 
         (
          [value] => abc 
          [result] => 1 
         ) 

        [subentry2_sub2] => Array 
         (
          [value] => abc 
          [result] => 1 
         ) 

       ) 

     )   
) 
+0

爲什麼'subentry1_sub1 = 2 + 2'? – billyonecan

+0

因爲它是數組中的兩倍: [entry1] => [subentry1] => [subentry1_sub1] =>結果 [entry2] => [subentry1] => [subentry1_sub1] =>結果 – WebWorker

回答

1

試試這個代碼。在這方面,我已經創建了一個新的array$sum這將增加與相同的密鑰和另一個數組$count相同子條目的孩子的的結果值,這將計數的次數每個鍵重複

<?php 
    $data = array('entry1'=>array(
     'subentry1'=> 
      array(
       'subentry1_sub1'=>array('value'=>'abc','result'=>2), 
       'subentry1_sub2'=>array('value'=>'abc','result'=>1) 
      ), 
     'subentry2'=> 
      array(
       'subentry2_sub1'=>array('value'=>'abc','result'=>1), 
       'subentry2_sub2'=>array('value'=>'abc','result'=>1) 
      ) 

     ), 
    'entry2'=>array(
     'subentry1'=> 
      array(
       'subentry1_sub1'=>array('value'=>'abc','result'=>2), 
       'subentry1_sub2'=>array('value'=>'abc','result'=>1) 
      ), 
     'subentry2'=> 
      array(
       'subentry2_sub1'=>array('value'=>'abc','result'=>1), 
       'subentry2_sub2'=>array('value'=>'abc','result'=>1) 
      ) 
     ) 
    ); 

$sum = array(); 
$repeat = array(); 

    foreach($data as $input){ 
     foreach($input as $array){ 
       foreach($array as $key=>$value){ 
         if(array_key_exists($key,$sum)){ 
         $repeat[$key] = $repeat[$key]+1; 
         $sum[$key] = $sum[$key] + $value['result']; 
         }else{ 
         $repeat[$key] = 1; 
         $sum[$key] = $value['result'];        
       }}}}      
echo "<pre>"; 
print_r($sum); 
print_r($repeat); 
foreach($sum as $key=>$value){ 
    echo $key. ' Average = '. $value/$repeat[$key]."</br>"; 
    } 

輸出

Array 
(
    [subentry1_sub1] => 4 
    [subentry1_sub2] => 2 
    [subentry2_sub1] => 2 
    [subentry2_sub2] => 2 
) 
Array 
(
    [subentry1_sub1] => 2 
    [subentry1_sub2] => 2 
    [subentry2_sub1] => 2 
    [subentry2_sub2] => 2 
) 


subentry1_sub1 Average = 2 
subentry1_sub2 Average = 1 
subentry2_sub1 Average = 1 
subentry2_sub2 Average = 1 

你現在可以很容易地計算平均值

注意:正如你所說,你正在計數發生subentry1_sub1等,所以我做了Ë相同的,所以它也將count鍵是否result存在與否

+0

謝謝!這正是我所搜索的!它運作良好。 – WebWorker

0

嘗試此類似,

<?php 
    $data=array('entry1'=>array(
     'subentry1'=> 
      array(
       'subentry1_sub1'=>array('value'=>'abc','result'=>3), 
       'subentry1_sub2'=>array('value'=>'abc','result'=>3) 
      ), 
     'subentry2'=> 
      array(
       'subentry2_sub1'=>array('value'=>'abc','result'=>2), 
       'subentry2_sub2'=>array('value'=>'abc','result'=>8) 
      ) 

     ), 
    'entry2'=>array(
     'subentry1'=> 
      array(
       'subentry1_sub1'=>array('value'=>'abc','result'=>6), 
       'subentry1_sub2'=>array('value'=>'abc','result'=>6) 
      ), 
     'subentry2'=> 
      array(
       'subentry2_sub1'=>array('value'=>'abc','result'=>10), 
       'subentry2_sub2'=>array('value'=>'abc','result'=>12) 
      ) 
     ) 
    ); 

    foreach($data as $k=>$v){ 
     echo "----------------$k---------------------\n"; 
     if(is_array($v)){ 
      foreach($v as $a=>$b){ 
       if(is_array($b)){ 
        echo $a.' average = '; 
        $c=array_keys($b);// now get *_sub* 
        $v1=isset($b[$c[0]]['result']) ? $b[$c[0]]['result'] : ''; 
        $v2=isset($b[$c[1]]['result']) ? $b[$c[1]]['result'] : ''; 
        echo ($v1+$v2)/2; 
        echo "\n"; 
       }     
      } 
     } 
    } 

Online Demo

+0

感謝您的回答。上面的例子只是一個摘錄。真實的數據要大得多。所以對密鑰進行硬編碼是行不通的。 – WebWorker

+0

試試我的更新回答和演示 –

0

在此期間,我發現了一個簡單有效的解決方案自己:

foreach ($data as $level2) { 
    foreach ($level2 as $level3) { 
     foreach ($level3 as $keyp => $level4) { 
      foreach ($level4 as $key => $value) { 
       if($key == 'result') $stats[$keyp] += $value; 
      } 
     } 
    } 
} 

隨着你得到總在一個新的數組$stats每一個關鍵。

但一定要從user1234也簽出解決方案。它工作得很好,已經包含了平均值的計算。 https://stackoverflow.com/a/39292593/2466703