首页 > PHP输出表格

PHP输出表格

现有数据如下:

麵包紙杯 DPK219 VAC001 1.00
麵包紙杯 DPK219 VBW001 1.00
罐裝蟹肉 1磅/罐 RCN061-1 VAC001 2.00
罐裝蟹肉 1磅/罐 RCN061-1 VBW001 2.00
牛淋片5mm RFB300-2 VAC001 3.00
牛淋片5mm RFB300-2 VBW001 3.00
厚牛淋片 RFB300-3 VAC001 4.00
厚牛淋片 RFB300-3 VBW001 4.00

需求是输出一个表格(类似excel),所有同款产品的信息用一行输出,一共四行,如下:

麵包紙杯 1.00 1.00
罐裝蟹肉 1磅/罐 2.00 2.00
牛淋片5mm 3.00 3.00
厚牛淋片 4.00 4.00

菜鸟菜问题,勿怪。


为了给你一个例子就混编了。

<?php
    $info = array(
        array('catogary' => '麵包紙杯', 'typeA' => 'DPK219', 'typeB' => 'VAC001', 'price' => '1.00'),
        array('catogary' => '麵包紙杯', 'typeA' => 'DPK219', 'typeB' => 'VBW001', 'price' => '1.00'),
        array('catogary' => '罐裝蟹肉 ', 'typeA' => 'RCN061-1', 'typeB' => 'VAC001', 'price' => '2.00'),
        array('catogary' => '罐裝蟹肉 ', 'typeA' => 'RCN061-1', 'typeB' => 'VBW001', 'price' => '2.00'),
        array('catogary' => '牛淋片5mm', 'typeA' => 'RFB300-2', 'typeB' => 'VAC001', 'price' => '3.00'),
        array('catogary' => '牛淋片5mm', 'typeA' => 'RFB300-2', 'typeB' => 'VBW001', 'price' => '3.00'),
        array('catogary' => '厚牛淋片', 'typeA' => 'RFB300-3', 'typeB' => 'VAC001', 'price' => '4.00'),
        array('catogary' => '厚牛淋片', 'typeA' => 'RFB300-3', 'typeB' => 'VBW001', 'price' => '4.00'),
    );

    $tableInfo = array();

    // $tableInfo = array_column($info, 'catogary', 'price');//if php version > 5.5

    foreach($info as $item)
        $tableInfo[$item['catogary']] = $item['price'];
?>
    <table>
<?php
    foreach($tableInfo as $catogary => $price) {
?>
        <tr>
            <td><?=$catogary?></td>
            <td><?=$price?></td>
            <td><?=$price?></td>
        </tr>
    </tbale>
<?php
    }

大体讲下思路,先把数据存在数组 info 内,然后对数组进行过滤,取出需要展示在表格内的有用数据到一个新数组 tableInfo 内。接着,遍历新数组将数据输出到表格 table 内即可。效果图如下:

【热门文章】
【热门文章】