malios/php-to-ascii-table

一个用于生成纯文本表格的PHP库。

v3.0.0 2022-02-13 20:15 UTC

This package is not auto-updated.

Last update: 2024-09-17 09:30:42 UTC


README

一个用于生成纯文本表格的小型PHP库。

example table

入门

先决条件

  • PHP >= 7
  • ext-mbstring
  • 可选安装 php-ds 扩展(推荐)。

安装

通过composer安装

$ composer require malios/php-to-ascii-table

用法

<?php
    $builder = new \AsciiTable\Builder();
    
    $builder->addRows([
        [
            'Order No' => 'A0001',
            'Product Name' => 'Intel CPU',
            'Price' => 700.00,
            'Quantity' => 1
        ],
        [
            'Order No' => 'A0002',
            'Product Name' => 'Hard disk 10TB',
            'Price' => 500.00,
            'Quantity' => 2
        ],
        [
            'Order No' => 'A0003',
            'Product Name' => 'Dell Laptop',
            'Price' => 11600.00,
            'Quantity' => 8
        ],
        [
            'Order No' => 'A0004',
            'Product Name' => 'Intel CPU',
            'Price' => 5200.00,
            'Quantity' => 3
        ]
    ]);
    
    $builder->addRow([
        'Order No' => 'A0005',
        'Product Name' => 'A4Tech Mouse',
        'Price' => 100.00,
        'Quantity' => 10
    ]);

    $builder->setTitle('Product List');
    
    echo $builder->renderTable();
   

    // Show only some fields
    
    $builder->showColumns(['Order No', 'Product Name', 'Quantity']);
    
    echo $builder->renderTable();
    

从对象构建表格

你可以从实现JsonSerializable接口的任何对象构建表格。

<?php
    
    class Person implements \JsonSerializable 
    {
        private $name;
        
        private $age;
        
        public function __construct(string $name, int $age) {
            $this->name = $name;
            $this->age = $age;
        }
        
        public function jsonSerialize()
        {
            return [
                'name' => $this->name,
                'age' => $this->age
            ];
        }
    }
    
    $builder = new \AsciiTable\Builder();
    
    $builder->addRow(new Person('John', 25));
    $builder->addRow(new Person('Bill', 30));
    
    echo $builder->renderTable();

贡献

所有贡献者都欢迎。你可以打开一个新问题或提交一个pull request。详见 CONTRIBUTING.md

许可证

本项目采用MIT许可证 - 请参阅 LICENSE.md 文件以获取详细信息。