如何将此数组中的数据排序为HTML表?

时间:2014-05-25 04:02:43

标签: php arrays

我有这个数组输出:

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  |
====================================================================

2 个答案:

答案 0 :(得分:0)

您必须使用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>

答案 1 :(得分:0)

您也可以尝试这种方式

 <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>