array-view-php

Форк
0

7 месяцев назад
7 месяцев назад
7 месяцев назад
7 месяцев назад
7 месяцев назад
7 месяцев назад
7 месяцев назад
7 месяцев назад
7 месяцев назад
README.md

Array View PHP

Packagist PHP Version Support Scrutinizer Code Quality Coverage Status Build and test License: MIT

Array View is a PHP library that provides a powerful set of utilities for working with arrays in a versatile and efficient manner. These classes enable developers to create views of arrays, manipulate data with ease, and select specific elements using index lists, masks, and slice parameters.

Array View offers a Python-like slicing experience for efficient data manipulation and selection of array elements.

Features

  • Create array views for easy data manipulation.
  • Select elements using Python-like slice notation.
  • Handle array slicing operations with ease.
  • Enable efficient selection of elements using index lists and boolean masks.

How to install to your project

composer require smoren/array-view

Usage

Quick examples

Indexing and Slicing

use Smoren\ArrayView\Views\ArrayView;
$originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$view = ArrayView::toView($originalArray);
$view['1:7:2']; // [2, 4, 6]
$view[':3']; // [1, 2, 3]
$view['::-1']; // [9, 8, 7, 6, 5, 4, 3, 2, 1]
$view[2]; // 3
$view[4]; // 5
$view[-1]; // 9
$view[-2]; // 8
$view['1:7:2'] = [22, 44, 66];
print_r($originalArray); // [1, 22, 3, 44, 5, 66, 7, 8, 9]

Subviews

use Smoren\ArrayView\Selectors\IndexListSelector;
use Smoren\ArrayView\Selectors\MaskSelector;
use Smoren\ArrayView\Selectors\SliceSelector;
use Smoren\ArrayView\Views\ArrayView;
$originalArray = [1, 2, 3, 4, 5];
$view = ArrayView::toView($originalArray);
$view->subview(new MaskSelector([true, false, true, false, true]))->toArray(); // [1, 3, 5]
$view->subview(new IndexListSelector([1, 2, 4]))->toArray(); // [2, 3, 5]
$view->subview(new SliceSelector('::-1'))->toArray(); // [5, 4, 3, 2, 1]
$view->subview([true, false, true, false, true])->toArray(); // [1, 3, 5]
$view->subview([1, 2, 4])->toArray(); // [2, 3, 5]
$view->subview('::-1')->toArray(); // [5, 4, 3, 2, 1]
$view->subview(new MaskSelector([true, false, true, false, true]))->apply(fn ($x) => x * 10);
print_r($originalArray); // [10, 2, 30, 4, 50]

Subarrays

use Smoren\ArrayView\Selectors\IndexListSelector;
use Smoren\ArrayView\Selectors\MaskSelector;
use Smoren\ArrayView\Selectors\SliceSelector;
use Smoren\ArrayView\Views\ArrayView;
$originalArray = [1, 2, 3, 4, 5];
$view = ArrayView::toView($originalArray);
$view[new MaskSelector([true, false, true, false, true])]; // [1, 3, 5]
$view[new IndexListSelector([1, 2, 4])]; // [2, 3, 5]
$view[new SliceSelector('::-1')]; // [5, 4, 3, 2, 1]
$view[[true, false, true, false, true]]; // [1, 3, 5]
$view[[1, 2, 4]]; // [2, 3, 5]
$view['::-1']; // [5, 4, 3, 2, 1]
$view[new MaskSelector([true, false, true, false, true])] = [10, 30, 50];
print_r($originalArray); // [10, 2, 30, 4, 50]

Combining Subviews

use Smoren\ArrayView\Selectors\IndexListSelector;
use Smoren\ArrayView\Selectors\MaskSelector;
use Smoren\ArrayView\Selectors\SliceSelector;
use Smoren\ArrayView\Views\ArrayView;
$originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$subview = ArrayView::toView($originalArray)
->subview(new SliceSelector('::2')) // [1, 3, 5, 7, 9]
->subview(new MaskSelector([true, false, true, true, true])) // [1, 5, 7, 9]
->subview(new IndexListSelector([0, 1, 2])) // [1, 5, 7]
->subview(new SliceSelector('1:')); // [5, 7]
$subview[':'] = [55, 77];
print_r($originalArray); // [1, 2, 3, 4, 55, 6, 77, 8, 9, 10]
$originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$subview = ArrayView::toView($originalArray)
->subview('::2') // [1, 3, 5, 7, 9]
->subview([true, false, true, true, true]) // [1, 5, 7, 9]
->subview([0, 1, 2]) // [1, 5, 7]
->subview('1:'); // [5, 7]
$subview[':'] = [55, 77];
print_r($originalArray); // [1, 2, 3, 4, 55, 6, 77, 8, 9, 10]

Selectors Pipe

use Smoren\ArrayView\Selectors\IndexListSelector;
use Smoren\ArrayView\Selectors\MaskSelector;
use Smoren\ArrayView\Selectors\SliceSelector;
use Smoren\ArrayView\Views\ArrayView;
$originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$selector = new PipeSelector([
new SliceSelector('::2'),
new MaskSelector([true, false, true, true, true]),
new IndexListSelector([0, 1, 2]),
new SliceSelector('1:'),
]);
$view = ArrayView::toView($originalArray);
$subview = $view->subview($selector);
print_r($subview[':']); // [5, 7]
$subview[':'] = [55, 77];
print_r($originalArray); // [1, 2, 3, 4, 55, 6, 77, 8, 9, 10]

Documentation

For detailed documentation and usage examples, please refer to the API documentation.

Unit testing

composer install
composer test-init
composer test

Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request on the GitHub repository.

Standards

ArrayView conforms to the following standards:

License

ArrayView PHP is licensed under the MIT License.

Описание

Create array views for easy data manipulation, select elements using Python-like slice notation, enable efficient selection of elements using index lists and boolean masks.

Языки

PHP

Сообщить о нарушении

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.