Php 如何将此数组中的数据排序到HTML表中?
我有以下数组输出:Php 如何将此数组中的数据排序到HTML表中?,php,arrays,Php,Arrays,我有以下数组输出: Array ( [LB] => Array ( [0] => Array ( [desc] => 15W LUB OIL CASTROL 5320 [uprice] => 431.29 [qty] => 1 [discount] => 0
Array
(
[LB] => Array
(
[0] => Array
(
[desc] => 15W LUB OIL CASTROL 5320
[uprice] => 431.29
[qty] => 1
[discount] => 0
[subtotal] => 431.29
)
)
[MS] => Array
(
[0] => Array
(
[desc] => B/C 0001 Aircond
[uprice] => 132.00
[qty] => 3
[discount] => 0
[subtotal] => 396.00
)
[1] => Array
(
[desc] => BODY PART (FENDER)
[uprice] => 10981.20
[qty] => 2
[discount] => 0
[subtotal] => 21962.40
)
[2] => Array
(
[desc] => BODY PART (BONET)
[uprice] => 33821.93
[qty] => 2
[discount] => 0
[subtotal] => 67643.86
)
[3] => Array
(
[desc] => SKRU 14MM
[uprice] => 1091.30
[qty] => 14
[discount] => 0
[subtotal] => 15278.20
)
)
[WS] => Array
(
[0] => Array
(
[desc] => 15000km basic service
[uprice] => 1205.67
[qty] => 1
[discount] => 0
[subtotal] => 1205.67
)
)
)
我真的不知道怎么做。我的问题是如何将数组数据排序到HTML表中?输出如下所示:
| DESC | QTY | U/PRICE | DISCOUNT | SUBTOTAL |
====================================================================
| LB | | | | |
| 15W LUB OIL CASTROL 5320 | 1 | 431.29 | 0 | 431.29 |
| | | | | |
| MS | | | | |
| B/C 0001 Aircond | 3 | 132.00 | 0 | 396.00 |
| BODY PART (FENDER) | 2 | 10981.20| 0 | 21962.40 |
| BODY PART (BONET) | 2 | 33821.93| 0 | 67643.86 |
| SKRU 14MM | 14 | 1091.30 | 0 | 15278.20 |
| | | | | |
| WS | | | | |
| 15000km basic service | 1 | 1205.67 | 0 | 1205.67 |
====================================================================
必须使用foreach循环。我认为这里一定有错误,但这对你们来说是正确的原则。像这样的
<table>
<?php
foreach ($array as $mainarray){
echo "<tr><td>".$mainarray['name']."</td></tr>"; //outpout row with value "LB", "MS", "WA"
foreach ($mainarray['value'] as $item){
echo "<tr><td>".$item[1]."</td>";
echo "<td>".$item[2]."</td>";
echo "<td>".$item[3]."</td>";
echo "<td>".$item[4]."</td>";
echo "<td>".$item[5]."</td></tr>";
}
}?>
</table>
您也可以这样尝试
<table>
<?php
$names=['LB','MS','WB'];// or you can set name by looping main array
$i=0;
foreach ($array as $singlearray){
echo "<tr><td>$names[$i]</td></tr>"; //set names
foreach ($singlearray as $row){
echo "<tr><td>".$row['desc']."</td>";
echo "<td>".$row['price']."</td>";
echo "<td>".$row['qty']."</td>";
echo "<td>".$row['discount']."</td>";
echo "<td>".$row['subtotal']."</td></tr>";
}
$i++;
}?>
</table>
您可以使用foreach
循环开始。到目前为止你都做了些什么?如果一个人太懒了,不愿意自己做一些基本的搜索并尝试一些东西,那么他们不太可能从你填鸭式的代码中学到任何东西。