<?php foreach((array)$query as $row):?>
<td><?php echo $row->date ?></td>
<td><?php echo $row->cost ?></td>
<?php endforeach ;?>
我想在$row->cost
$row->date=02-01-2016
中汇总所有值,就像在表格中一样
感谢。
答案 0 :(得分:1)
echo array_sum(array_filter(array_map(function($row){
if($row->date == '02-01-2016')
return $row->cost;
}, $array)));
您可以使用
array_map
:然后删除空白索引array_filter
:最后总结费用注意:如果您从数据库获得结果,您可以在那里进行总结(见下文):
$db->query("SELECT SUM(costs) FROM table_name WHERE `date`='2016-01-02'");
echo $db->fetchColumn();
答案 1 :(得分:0)
尝试以下代码:
<?php
$total = 0;
foreach((array)$query as $row):
$date = $row->date;
$cost = $row->cost;
if(date("M-d-Y", strtotime($date))==date("M-d-Y")) {
$total = $total+$cost;
}
?>
<td><?php echo $date ?></td>
<td><?php echo $cost ?></td>
<?php endforeach; ?>
<td colspan="2"><?php echo $total; ?></td>
答案 2 :(得分:0)
<?php
$tot = 0;
foreach( (array)$query as $row ) {
if( $row->date=='02-01-2016' ) $tot = $tot+$row->cost;
?>
<tr>
<td><?php echo $row->date ?></td>
<td><?php echo $row->cost ?></td>
</tr>
<?php } ?>
<tr><td colspan="2"><?php echo $tot; ?></td></tr>
显然,如果$row->date
格式为dd-mm-yyyy
答案 3 :(得分:0)
<table border=1>
<?php
$tot = 0;
foreach($query as $row ) {
if( $row->date=='2016-01-02' ) $tot += $row->cost;
?>
<tr>
<td><?php echo $row->date ?></td>
<td><?php echo $row->cost ?></td>
</tr>
<?php } ?>
<tr><td></td><td><?php echo $total; ?></td></tr>
</table>
它会对你有所帮助。