Skip to content

Commit ec93b7b

Browse files
committed
README updated
1 parent 477d8c4 commit ec93b7b

File tree

1 file changed

+89
-3
lines changed

1 file changed

+89
-3
lines changed

README.md

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,98 @@
55
![Build and test](https://github.com/Smoren/array-view-php/actions/workflows/test_master.yml/badge.svg)
66
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
77

8-
TODO
8+
**Array View** is a PHP library that provides a powerful set of utilities for working with arrays in
9+
a versatile and efficient manner. These classes enable developers to create views of arrays, manipulate data with ease,
10+
and select specific elements using index lists, masks, and slice parameters.
11+
12+
Array View offers a Python-like slicing experience for efficient data manipulation and selection of array elements.
13+
14+
## Features
15+
- Create array views for easy data manipulation.
16+
- Select elements using [Python-like slice notation](https://www.geeksforgeeks.org/python-list-slicing/).
17+
- Handle array slicing operations with ease.
18+
- Enable efficient selection of elements using index lists and boolean masks.
19+
920

1021
## How to install to your project
11-
```
22+
```bash
1223
composer require smoren/array-view
1324
```
1425

1526
## Usage
16-
TODO
27+
## Quick examples
28+
### Slicing
29+
```php
30+
use Smoren\ArrayView\Views\ArrayView;
31+
32+
$originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];
33+
$originalView = ArrayView::toView($originalArray);
34+
35+
$originalView['1:7:2']; // [2, 4, 6]
36+
$originalView[':3']; // [1, 2, 3]
37+
$originalView['::-1']; // [9, 8, 7, 6, 5, 4, 3, 2, 1]
38+
39+
$originalView[2]; // 3
40+
$originalView[4]; // 5
41+
42+
$originalView['1:7:2'] = [22, 44, 66];
43+
print_r($originalView); // [1, 22, 3, 44, 5, 66, 7, 8, 9]
44+
```
45+
46+
### Subviews
47+
```php
48+
use Smoren\ArrayView\Selectors\IndexListSelector;
49+
use Smoren\ArrayView\Selectors\MaskSelector;
50+
use Smoren\ArrayView\Selectors\SliceSelector;
51+
use Smoren\ArrayView\Views\ArrayView;
52+
53+
$originalArray = [1, 2, 3, 4, 5];
54+
$originalView = ArrayView::toView($originalArray);
55+
56+
$originalView.subview(new MaskSelector([true, false, true, false, true])).toArray(); // [1, 3, 5]
57+
$originalView.subview(new IndexListSelector([1, 2, 4])).toArray(); // [2, 3, 5]
58+
$originalView.subview(new SliceSelector('::-1')).toArray(); // [5, 4, 3, 2, 1]
59+
60+
$originalView.subview(new MaskSelector([true, false, true, false, true])).apply(fn ($x) => x * 10);
61+
print_r(originalArray); // [10, 2, 30, 4, 50]
62+
```
63+
64+
### Subarrays
65+
```php
66+
use Smoren\ArrayView\Selectors\IndexListSelector;
67+
use Smoren\ArrayView\Selectors\MaskSelector;
68+
use Smoren\ArrayView\Selectors\SliceSelector;
69+
use Smoren\ArrayView\Views\ArrayView;
70+
71+
$originalArray = [1, 2, 3, 4, 5];
72+
$originalView = ArrayView::toView($originalArray);
73+
74+
$originalView[new MaskSelector([true, false, true, false, true])]; // [1, 3, 5]
75+
$originalView[new IndexListSelector([1, 2, 4])]; // [2, 3, 5]
76+
$originalView[new SliceSelector('::-1')]; // [5, 4, 3, 2, 1]
77+
78+
$originalView[new MaskSelector([true, false, true, false, true])] = [10, 30, 50];
79+
print_r(originalArray); // [10, 2, 30, 4, 50]
80+
```
81+
82+
### Combining subviews
83+
```php
84+
use Smoren\ArrayView\Selectors\IndexListSelector;
85+
use Smoren\ArrayView\Selectors\MaskSelector;
86+
use Smoren\ArrayView\Selectors\SliceSelector;
87+
use Smoren\ArrayView\Views\ArrayView;
88+
89+
const $originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
90+
91+
const $subview = ArrayView::toView(originalArray)
92+
->subview('::2') // [1, 3, 5, 7, 9]
93+
->subview(new MaskSelector([true, false, true, true, true])) // [1, 5, 7, 9]
94+
->subview(new IndexListSelector([0, 1, 2])) // [1, 5, 7]
95+
->subview('1:'); // [5, 7]
96+
97+
$subview[':'] = [55, 77];
98+
print_r($originalArray); // [1, 2, 3, 4, 55, 6, 77, 8, 9, 10]
99+
```
17100

18101
## Unit testing
19102
```
@@ -22,6 +105,9 @@ composer test-init
22105
composer test
23106
```
24107

108+
## Contributing
109+
Contributions are welcome! Feel free to open an issue or submit a pull request on the [GitHub repository](https://github.com/Smoren/array-view-ts).
110+
25111
## Standards
26112
ArrayView conforms to the following standards:
27113

0 commit comments

Comments
 (0)