Home > Backend Development > PHP Tutorial > PHP输出表格

PHP输出表格

WBOY
Release: 2016-06-06 20:46:04
Original
1598 people have browsed it

现有数据如下:

麵包紙杯 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

菜鸟菜问题,勿怪。

回复内容:

现有数据如下:

麵包紙杯 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

菜鸟菜问题,勿怪。

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

<code><?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>
    
<?php }
</code>

<p>大体讲下思路,先把数据存在数组 <code>info</code> 内,然后对数组进行过滤,取出需要展示在表格内的有用数据到一个新数组 <code>tableInfo</code> 内。接着,遍历新数组将数据输出到表格 <code>table</code> 内即可。效果图如下:<br><img  data-src="http://segmentfault.com/img/bVccbs" alt="PHP输出表格" ></p>

                            </table></code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template