挑战:显示带有聚合结果的php数组
我即将为这个挑战而流血……任何人都可以帮助我理解如何解决这个问题。我不懂逻辑。我将为您提供详细信息,并为您提供要使用的输入变量,这些变量是动态生成的。下面的记录是男,女,男,女挑战:显示带有聚合结果的php数组,php,Php,我即将为这个挑战而流血……任何人都可以帮助我理解如何解决这个问题。我不懂逻辑。我将为您提供详细信息,并为您提供要使用的输入变量,这些变量是动态生成的。下面的记录是男,女,男,女 $data=array(1,5,10,4,3,4,1,1,1,'2); $indexOfSubTotalColumns=array(3,6,9,12,15,18,21); //This means on the 3, 6th ...column of the HTML output, a summary is to h
$data=array(1,5,10,4,3,4,1,1,1,'2);
$indexOfSubTotalColumns=array(3,6,9,12,15,18,21);
//This means on the 3, 6th ...column of the HTML output, a summary is to happen. That is 1+5 will be 6 on column 3. 10 +4 will be 14 on column 6.
$groupTotalColumns=array(9,21);
//the 9th column will display sum of all columns before it. and 21 from col 10 on words, excluding the subtotals calculated or if you want.
在下面的代码中,我可以计算每一个的小计,但不能计算动态的总计。首先,将哑数据添加到数据数组中,使其等于HTML列的数量
$previousValues=array();
foreach($data as $value){
if(in_array($i,$indexOfSubTotalColumns)){
$rows.="<td>".array_sum($previousValues)."</td>";
$rows.="<td>$value</td>";
$previousValues=array();
array_push($previousValues,$value);
$i++;
//
}
else{//not a subtotal
$rows.="<td>$value</td>";
array_push($previousValues,$value);
}
$i++;
//if $i reaches number of columns i.e. 21, close current row and open new
}
echo $rows;
$previousValues=array();
foreach(数据为$value){
if(在数组中($i,$indexOfSubTotalColumns)){
$rows.=''.array_sum($previousValues)。'';
$rows.=“$value”;
$previousValues=array();
数组\u push($previousValues,$value);
$i++;
//
}
否则{//不是小计
$rows.=“$value”;
数组\u push($previousValues,$value);
}
$i++;
//如果$i达到列数,即21,则关闭当前行并打开新的
}
echo$行;
以上代码将我输出此布局:
我无法计算总数。您的数组无效,数组键必须是唯一的,php将按如下方式处理您的
$data
数组:$data=array('M'=>1,'F'=>2)
并将忽略或覆盖前面的所有元素Hanks Hassan…请编辑帖子。