這是所有數組專家的問題。我有一個多維數組,其結果爲數字(可以是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
)
)
)
)
爲什麼'subentry1_sub1 = 2 + 2'? – billyonecan
因爲它是數組中的兩倍: [entry1] => [subentry1] => [subentry1_sub1] =>結果 [entry2] => [subentry1] => [subentry1_sub1] =>結果 – WebWorker