krypt0nn/table

Simple library to realize table

0.1.0 2021-04-03 18:05 UTC

This package is auto-updated.

Last update: 2024-05-29 05:26:34 UTC


README

table - небольшая библиотека для работы с таблицами строк на PHP 7.4+

Установка

composer require krypt0nn/table

Пример работы

Создание таблицы

<?php

use Table\Table;

$table = new Table (['id', 'name'], [
    [0, 'Hello'],
    [1, 'from'],
    [2, 'Russia!']
]);
<?php

use Table\Table;

$table = new Table;

$table->columns (['id', 'name'])->items ([
    [0, 'Hello'],
    [1, 'from'],
    [2, 'Russia!']
]);

Получение заголовков, элементов и их количества:

<?php

print_r ($table->columns ());

print_r ($table->items ());

echo $table->size ();
Array
(
    [0] => id
    [1] => name
)
Array
(
    [0] => Array
        (
            [0] => 0
            [1] => Hello
        )

    [1] => Array
        (
            [0] => 1
            [1] => from
        )

    [2] => Array
        (
            [0] => 2
            [1] => Russia!
        )

)
3

Вывод части элементов:

<?php

$table->foreach (function ($item)
{
    echo $item[1] .' ';
});
Hello from Russia!

Фильтрация элементов:

<?php

$table->where (function ($item)
{
    return $item[1] == 'hello';
});

Вывод массива элементов:

<?php

print_r ($table->get ());
Array
(
    [0] => Array
        (
            [id] => 0      
            [name] => Hello
        )

    [1] => Array
        (
            [id] => 1
            [name] => from
        )

    [2] => Array
        (
            [id] => 2
            [name] => Russia!
        )

)

Добавление элементов:

<?php

$table->push ([3, 'Alalalalala']);

$table->merge ([
    [4, 'Ololo'],
    [5. 'Olo lo'],
    [6, 'Lo']
]);

Кодирование и декодирование таблицы:

<?php

$table->delimiter = "\r\n";

file_put_contents ('table', $table->encode ());

$table = (new Table)->decode (file_get_contents ('table'));

table:

2
id
name
0
Hello
1
from
2
Russia!

Автор: Подвирный Никита