在类别概述中,我需要总结所有子类别中研究的所有项目。
我在foreach()中有一个函数countitemsinsubcat(),它为每个子类别($ id_cat)返回一个数组'$ value'。
foreach ($subcategory as $row) {
$value =& countitemsinsubcat($id_cat);
$all_values_found [] = $value;
}
对于包含2个子类别的类别,这些是$ all_values_found:
Array (
[0] => Array(
[Istudied] => 0
[Itotal] => 1
)
[1] => Array (
[Istudied] => 1
[Itotal] => 4
)
)
在类别概述中,我想总结每个子类别的数组的值,并得到一个'总'数组,如下所示:
Array
(
[Istudied] => 1
[Itotal] => 5
)
有关如何做的任何建议?
答案 0 :(得分:0)
看一下这段代码[PHP]:
//The magic happens here:
function concatit($v, $w)
{
$v[Istudied] += $w[Istudied];
$v[Itotal] += $w[Itotal];
return $v;
}
//Declaring the array.
$a = array (array(
Istudied => 0,
Itotal => 1
),array (
Istudied => 1,
Itotal => 4
)
);
//Making a call to the 'concatit' function declared above from within array_reduce.
$d = array_reduce($a, "concatit");
//Now $d contains the array as you wanted it.
echo $d[Istudied].' '.$d[Itotal];
如果您需要进一步澄清,请告诉我们!享受!