如何使用PHP中的foreach循环仅回显特定的多个相同记录一次? [英] How to echo specific multiple same records only once using foreach loops in PHP?
问题描述
我在PHP中使用foreach循环获得了这些输出.现在,foreach内部的输出如下所示.
I have these output using foreach loop in PHP. Right now the output inside foreach is like below.
PHP代码
<table>
<thead>
<tr>
<th>ACCOUNT NUMBER</th>
<th>CATEGORY</th>
<th>AMOUNT</th>
</tr>
</thead>
<tbody>
<?php
$sqlTxt = "SELECT * FROM table WHERE column = 'Fruits' ";
$sql = ORACLE::getInstance()->FetchArray($sqlTxt);
if(count($sql) > 0)
{
foreach($sql as $row)
{
?>
<tr>
<td><?php echo $row["ACCOUNT_NUMBER"] ;?></td>
<td><?php echo $row["CATEGORY"]; ?></td>
<td><?php echo $row["AMOUNT"]; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
上面代码的输出是这样
Account Number Category Amount
1001 Apple 30.00
1001 Mango 30.00
1001 Blueberry 30.00
1002 Durian 40.00
1002 Pineapple 40.00
1002 Jackfruit 40.00
1002 Lime 40.00
1003 Lemon 50.00
1003 Orange 50.00
我的问题是,如何仅在类别保持循环的同时回显帐号和金额?
My questions is how can I echo Account Number and Amount one time only while category keeps the looping ?
我的目标是看起来像这样.在每次昏迷后添加br标签.
My goal is to look like this. To add br tag after each comas.
Account Number Category Amount
1001 Apple, 30.00
Mango,
Blueberry
1002 Durian, 40.00
Pineapple,
Jackfruit,
Lime
1003 Lemon, 50.00
Orange
到目前为止,我在代码中所拥有的就是这样.
So far, what I have in my code is like this.
PHP代码
<table>
<thead>
<tr>
<th>ACCOUNT NUMBER</th>
<th>CATEGORY</th>
<th>AMOUNT</th>
</tr>
</thead>
<tbody>
<?php
$sqlTxt = "SELECT * FROM table WHERE column = 'Fruits' ";
$sql = ORACLE::getInstance()->FetchArray($sqlTxt);
if(count($sql) > 0)
{
$account_numbers = null;
$categorys = "";
$amounts = null;
foreach($sql as $row)
{
if($row['ACCOUNT_NUMBER'] !== $account_numbers)
{
if ($account_numbers !== null)
{
echo '</td></tr>';
}
echo "<tr>
<td>{$row['ACCOUNT_NUMBER']}</td>
<td>{$row['CATEGORY']}";
}
else {
echo "<br>{$selectionC['CATEGORY']}";
}
$account_numbers = $row['ACCOUNT_NUMBER'];
}
echo ' </td>
</tr>';
}
?>
</tbody>
</table>
我被困在这里.感谢有人可以帮助我解决这个问题.
I'm stuck here. Appreciate if someone can help me to solve this problem.
谢谢.
推荐答案
尝试类似的方法.
创建新数组,重新排序并对其进行修改.
Create new array,reorder and modified it.
PHP
<?php
$arr_old = array(
0 => array('ACCOUNT_NUMBER' => '1001', 'CATEGORY' => 'Apple', 'AMOUNT' => '30.00'),
1 => array('ACCOUNT_NUMBER' => '1001', 'CATEGORY' => 'Mango', 'AMOUNT' => '30.00'),
2 => array('ACCOUNT_NUMBER' => '1001', 'CATEGORY' => 'Blueberry', 'AMOUNT' => '30.00'),
3 => array('ACCOUNT_NUMBER' => '1002', 'CATEGORY' => 'Durian', 'AMOUNT' => '40.00'),
4 => array('ACCOUNT_NUMBER' => '1002', 'CATEGORY' => 'Pineapple', 'AMOUNT' => '40.00'),
5 => array('ACCOUNT_NUMBER' => '1002', 'CATEGORY' => 'Lime', 'AMOUNT' => '40.00'),
6 => array('ACCOUNT_NUMBER' => '1003', 'CATEGORY' => 'Lemon', 'AMOUNT' => '50.00'),
7 => array('ACCOUNT_NUMBER' => '1002', 'CATEGORY' => 'Jackfruit', 'AMOUNT' => '40.00'),
8 => array('ACCOUNT_NUMBER' => '1003', 'CATEGORY' => 'Orange', 'AMOUNT' => '50.00')
);
$arr = array();
foreach($arr_old as $key => $item)
{
if(!array_key_exists($item['ACCOUNT_NUMBER'],$arr)){
// Add new data in array with ACCOUNT_NUMBER as an index
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['ACCOUNT_NUMBER'] = $item['ACCOUNT_NUMBER'];
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['CATEGORY'] = $item['CATEGORY'];
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['AMOUNT'] = $item['AMOUNT'];
}else{
// Only alter category index
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['CATEGORY'] .= ",".$item['CATEGORY'];
}
}
ksort($arr, SORT_NUMERIC);
echo "<table>";
echo "<tr>";
echo "<td>ACCOUNT_NUMBER</td>";
echo "<td>CATEGORY</td>";
echo "<td>AMOUNT</td>";
echo "</tr>";
foreach($arr as $key => $item){
$xpl = explode(",",$item[$key]['CATEGORY']);
$n_category = "";
foreach($xpl as $b => $a){
$n_category .= ($b!=0) ? "<br>".$a : $a ;
}
echo "<tr>";
echo "<td>".$item[$key]['ACCOUNT_NUMBER']."</td>";
echo "<td>".$n_category."</td>";
echo "<td>".$item[$key]['AMOUNT']."</td>";
echo "</tr>";
// print_r($item);
}
echo "</table>";
这里的示例: http://sandbox.onlinephpfunctions.com/code/cfd9579dfe39ab9f6a33a >
And the example here : http://sandbox.onlinephpfunctions.com/code/cfd9579dfe39ab9fba6922209ceec1a33debbfc6
这篇关于如何使用PHP中的foreach循环仅回显特定的多个相同记录一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!