PHP Classes

PHP Array View: Manipulate arrays using Python-like slice notation

Recommend this page to a friend!
  Info   View files Documentation   View files View files (39)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-03-11 (4 days ago) RSS 2.0 feedNot enough user ratingsTotal: 10 This week: 10All time: 11,254 This week: 8Up
Version License PHP version Categories
array-view-php 1.0.0MIT/X Consortium ...5PHP 5, Data types
Description 

Author

This package can manipulate arrays using Python-like slice notation.

It provides several classes that implement interfaces to manipulate arrays in several ways.

Currently it can:

- 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

Picture of Smoren  Freelight
  Performance   Level  
Name: Smoren Freelight <contact>
Classes: 36 packages by
Country: Russian Federation Russian Federation
Innovation award
Innovation award
Nominee: 15x

Documentation

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

Slicing

use Smoren\ArrayView\Views\ArrayView;

$originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$originalView = ArrayView::toView($originalArray);

$originalView['1:7:2']; // [2, 4, 6]
$originalView[':3']; // [1, 2, 3]
$originalView['::-1']; // [9, 8, 7, 6, 5, 4, 3, 2, 1]

$originalView[2]; // 3
$originalView[4]; // 5

$originalView['1:7:2'] = [22, 44, 66];
print_r($originalView); // [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];
$originalView = ArrayView::toView($originalArray);

$originalView.subview(new MaskSelector([true, false, true, false, true])).toArray(); // [1, 3, 5]
$originalView.subview(new IndexListSelector([1, 2, 4])).toArray(); // [2, 3, 5]
$originalView.subview(new SliceSelector('::-1')).toArray(); // [5, 4, 3, 2, 1]
$originalView.subview('::-1').toArray(); // [5, 4, 3, 2, 1]

$originalView.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];
$originalView = ArrayView::toView($originalArray);

$originalView[new MaskSelector([true, false, true, false, true])]; // [1, 3, 5]
$originalView[new IndexListSelector([1, 2, 4])]; // [2, 3, 5]
$originalView[new SliceSelector('::-1')]; // [5, 4, 3, 2, 1]
$originalView['::-1']; // [5, 4, 3, 2, 1]

$originalView[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;

const $originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const $subview = ArrayView::toView(originalArray)
    ->subview('::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('1:');                                             // [5, 7]

$subview[':'] = [55, 77];
print_r($originalArray); // [1, 2, 3, 4, 55, 6, 77, 8, 9, 10]

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.


  Files folder image Files  
File Role Description
Files folder image.github (1 directory)
Files folder imagedocs (1 directory)
Files folder imagesrc (1 file, 6 directories)
Files folder imagetests (3 files, 2 directories)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file codeception.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file test_master.yml Data Auxiliary data

  Files folder image Files  /  docs  
File Role Description
Files folder imageimages (1 file)

  Files folder image Files  /  docs  /  images  
File Role Description
  Accessible without login Image file schemator-logo.png Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageExceptions (6 files)
Files folder imageInterfaces (5 files)
Files folder imageSelectors (3 files)
Files folder imageStructs (2 files)
Files folder imageTraits (1 file)
Files folder imageViews (4 files)
  Plain text file Util.php Class Class source

  Files folder image Files  /  src  /  Exceptions  
File Role Description
  Plain text file IndexError.php Class Class source
  Plain text file KeyError.php Class Class source
  Plain text file NotSupportedError.php Class Class source
  Plain text file ReadonlyError.php Class Class source
  Plain text file SizeError.php Class Class source
  Plain text file ValueError.php Class Class source

  Files folder image Files  /  src  /  Interfaces  
File Role Description
  Plain text file ArraySelectorInterface.php Class Class source
  Plain text file ArrayViewInterface.php Class Class source
  Plain text file IndexListSelectorInterface.php Class Class source
  Plain text file MaskSelectorInterface.php Class Class source
  Plain text file SliceSelectorInterface.php Class Class source

  Files folder image Files  /  src  /  Selectors  
File Role Description
  Plain text file IndexListSelector.php Class Class source
  Plain text file MaskSelector.php Class Class source
  Plain text file SliceSelector.php Class Class source

  Files folder image Files  /  src  /  Structs  
File Role Description
  Plain text file NormalizedSlice.php Class Class source
  Plain text file Slice.php Class Class source

  Files folder image Files  /  src  /  Traits  
File Role Description
  Plain text file ArrayViewAccessTrait.php Class Class source

  Files folder image Files  /  src  /  Views  
File Role Description
  Plain text file ArrayIndexListView.php Class Class source
  Plain text file ArrayMaskView.php Class Class source
  Plain text file ArraySliceView.php Class Class source
  Plain text file ArrayView.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageunit (2 directories)
Files folder image_support (1 file)
  Accessible without login Plain text file coding_standard.xml Data Auxiliary data
  Accessible without login Plain text file unit.suite.yml Data Auxiliary data
  Accessible without login Plain text file _bootstrap.php Aux. Auxiliary script

  Files folder image Files  /  tests  /  unit  
File Role Description
Files folder imageArrayView (5 files)
Files folder imageExamples (1 file)

  Files folder image Files  /  tests  /  unit  /  ArrayView  
File Role Description
  Plain text file ErrorsTest.php Class Class source
  Plain text file IndexTest.php Class Class source
  Plain text file ReadTest.php Class Class source
  Plain text file SliceTest.php Class Class source
  Plain text file WriteTest.php Class Class source

  Files folder image Files  /  tests  /  unit  /  Examples  
File Role Description
  Plain text file ExamplesTest.php Class Class source

  Files folder image Files  /  tests  /  _support  
File Role Description
  Plain text file UnitTester.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:10
This week:10
All time:11,254
This week:8Up