PHP code example of dbt / table

1. Go to this page and download the library: Download dbt/table library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dbt / table example snippets


use Dbt\Table\Table;
use Dbt\Table\Row;
use Dbt\Table\Cell;

/**
 * Standard construction.
 */
$table = new Table(
    new Row(
        new Cell('row 0 / cell 0'),
        new Cell('row 0 / cell 1')
    ),
    new Row(
        new Cell('row 1 / cell 0'),
        new Cell('row 1 / cell 1')
    )
);

/**
 * From array. 
 */
$table = Table::fromArray([
    ['row 0 / cell 0', 'row 0 / cell 1'],
    ['row 1 / cell 0', 'row 1 / cell 1'],
]);

/*
 * Rows must have the same length. This will throw a LengthException:
 */
$table = Table::fromArray([
    ['row 0, cell 0', 'row 0, cell 1'],
    ['row 1, cell 0'], // Too short!
]);

use Dbt\Table\Table;
use Dbt\Table\Row;
use Dbt\Table\Cell;

$table = Table::fromArray([['row 0 / cell 0'], ['row 1 / cell 0']]);

/**
 * @var int $rowIndex
 * @var \Dbt\Table\Row $row 
 */
foreach ($table as $rowIndex => $row) {
    var_dump(count($table), count($row));
    
    /**
     * @var int $cellIndex
     * @var \Dbt\Table\Cell $cell 
     */
    foreach ($row as $cellIndex => $cell) {
        var_dump($cell->value());
    }
}

$table->push(new Row(new Cell('row 2 / cell 0')));

use \Dbt\Table\Cell;

/**
 * This int will be cast to a string. 
 */
$cell = Cell::make(1);

/**
 * This array will be serialized to a JSON string. 
 */
$cell = Cell::make(['string', 1, 9.9, true]);