Documentation

ArraySliceView extends ArrayView
in package

Class representing a slice-based view of an array or another ArrayView for accessing elements within a specified slice range.

$source = [1, 2, 3, 4, 5];
$view = ArrayView::toView($source)->subview(new SliceSelector('::2'));
$view->toArray(); // [1, 3, 5]
Tags
template

T Type of array source elements.

extends

ArrayView<T>

Table of Contents

Methods

__construct()  : mixed
Constructor to create a new ArrayView.
apply()  : ArrayView<string|int, T>
Applies a transformation function to each element in the view.
applyWith()  : ArrayView<string|int, T>
Sets new values for the elements in the view.
count()  : int
Return size of the view.
filter()  : ArrayMaskView<string|int, T>
Filters the elements in the view based on a predicate function.
getIterator()  : Generator<int, T>
Return iterator to iterate the view elements.
is()  : MaskSelector
Checks if all elements in the view satisfy a given predicate function.
isReadonly()  : bool
Return true if view is readonly, otherwise false.
map()  : array<string|int, T>
Transforms each element of the array using the given callback function.
mapWith()  : array<string|int, mixed>
Transforms each pair of elements from the current array view and the provided data array using the given callback function.
match()  : MaskSelector
Checks if all elements in the view satisfy a given predicate function.
matchWith()  : MaskSelectorInterface
Compares the elements of the current ArrayView instance with another array or ArrayView using the provided comparator function.
offsetExists()  : bool
Check if the specified offset exists in the ArrayView object.
offsetGet()  : T|array<string|int, T>
Get the value at the specified offset in the ArrayView object.
offsetSet()  : void
Set the value at the specified offset in the ArrayView object.
offsetUnset()  : void
Unset the value at the specified offset in the array-like object.
set()  : ArrayView<string|int, T>
Sets new values for the elements in the view.
subview()  : ArrayViewInterface<string|int, T>
Returns a subview of this view based on a selector or string slice.
toArray()  : array<string|int, T>
Returns the array representation of the view.
toUnlinkedView()  : ArrayViewInterface<string|int, T>
Creates an unlinked from source ArrayView instance from the given source array or ArrayView.
toView()  : ArrayViewInterface<string|int, T>
Creates an ArrayView instance from the given source array or ArrayView.

Methods

__construct()

Constructor to create a new ArrayView.

public __construct(array<string|int, T>|ArrayViewInterface<string|int, T&$source[, bool|null $readonly = null ]) : mixed
  • If the source is not an ArrayView, a new ArrayView is created with the provided source.
  • If the source is an ArrayView and the readonly parameter is specified as true, a new readonly ArrayView is created.
  • If the source is an ArrayView and it is already readonly, the same ArrayView is returned.
Parameters
$source : array<string|int, T>|ArrayViewInterface<string|int, T>

The source array or view.

$readonly : bool|null = null

Flag indicating if the view is readonly.

Tags
throws
ValueError

if the array is not sequential.

throws
ReadonlyError

if the source is readonly and trying to create a non-readonly view.

see
ArrayView::toView()

for creating views.

apply()

Applies a transformation function to each element in the view.

public apply(callable $mapper) : ArrayView<string|int, T>
Example
$source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$subview = ArrayView::toView($source)->subview('::2'); // [1, 3, 5, 7, 9]

$subview->apply(fn ($x) => $x * 10);

$subview->toArray(); // [10, 30, 50, 70, 90]
$source; // [10, 2, 30, 4, 50, 6, 70, 8, 90, 10]
Parameters
$mapper : callable
Return values
ArrayView<string|int, T>

this view.

applyWith()

Sets new values for the elements in the view.

public applyWith(array<string|int, U>|ArrayViewInterface<string|int, U$data, callable $mapper) : ArrayView<string|int, T>
Example
$source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$subview = ArrayView::toView($source)->subview('::2'); // [1, 3, 5, 7, 9]

$data = [9, 27, 45, 63, 81];

$subview->applyWith($data, fn ($lhs, $rhs) => $lhs + $rhs);
$subview->toArray(); // [10, 30, 50, 70, 90]

$source; // [10, 2, 30, 4, 50, 6, 70, 8, 90, 10]
Parameters
$data : array<string|int, U>|ArrayViewInterface<string|int, U>
$mapper : callable
Tags
template

U Type of $data items.

throws
ValueError

if the $data is not sequential array.

throws
SizeError

if size of $data not equals to size of the view.

Return values
ArrayView<string|int, T>

this view.

count()

Return size of the view.

public count() : int
Example
$source = [1, 2, 3, 4, 5];

$subview = ArrayView::toView($source)->subview('::2'); // [1, 3, 5]
count($subview); // 3
Return values
int

filter()

Filters the elements in the view based on a predicate function.

public filter(callable $predicate) : ArrayMaskView<string|int, T>
Example
$source = [1, 2, 3, 4, 5, 6];
$view = ArrayView::toView($source);

$filtered = $view->filter(fn ($x) => $x % 2 === 0);
$filtered->toArray(); // [2, 4, 6]

$filtered[':'] = [20, 40, 60];
$filtered->toArray(); // [20, 40, 60]
$source; // [1, 20, 3, 40, 5, 60]
Parameters
$predicate : callable
Return values
ArrayMaskView<string|int, T>

A new view with elements that satisfy the predicate.

getIterator()

Return iterator to iterate the view elements.

public getIterator() : Generator<int, T>
Example
$source = [1, 2, 3, 4, 5];
$subview = ArrayView::toView($source)->subview('::2'); // [1, 3, 5]

foreach ($subview as $item) {
    // 1, 3, 5
}

print_r([...$subview]); // [1, 3, 5]
Return values
Generator<int, T>

is()

Checks if all elements in the view satisfy a given predicate function.

public is(callable $predicate) : MaskSelector
Example
$source = [1, 2, 3, 4, 5, 6];
$view = ArrayView::toView($source);

$mask = $view->is(fn ($x) => $x % 2 === 0);
$mask->getValue(); // [false, true, false, true, false, true]

$view->subview($mask)->toArray(); // [2, 4, 6]
$view[$mask]; // [2, 4, 6]

$view[$mask] = [20, 40, 60];
$source; // [1, 20, 3, 40, 5, 60]
Parameters
$predicate : callable
Tags
see
ArrayViewInterface::match()

Full synonim.

Return values
MaskSelector

Boolean mask for selecting elements that satisfy the predicate.

isReadonly()

Return true if view is readonly, otherwise false.

public isReadonly() : bool
Example
$source = [1, 2, 3, 4, 5];

$readonlyView = ArrayView::toView($source, true);
$readonlyView->isReadonly(); // true

$readonlySubview = ArrayView::toView($source)->subview('::2', true);
$readonlySubview->isReadonly(); // true

$view = ArrayView::toView($source);
$view->isReadonly(); // false

$subview = ArrayView::toView($source)->subview('::2');
$subview->isReadonly(); // false
Return values
bool

map()

Transforms each element of the array using the given callback function.

public map(callable $mapper) : array<string|int, T>

The callback function receives two parameters: the current element of the array and its index.

Example
$source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$subview = ArrayView::toView($source)->subview('::2'); // [1, 3, 5, 7, 9]

$subview->map(fn ($x) => $x * 10); // [10, 30, 50, 70, 90]
Parameters
$mapper : callable
Return values
array<string|int, T>

New array with transformed elements of this view.

mapWith()

Transforms each pair of elements from the current array view and the provided data array using the given callback function.

public mapWith(array<string|int, U>|ArrayViewInterface<string|int, U>|U $data, callable $mapper) : array<string|int, mixed>

The callback function receives three parameters: the current element of the current array view, the corresponding element of the data array, and the index.

Example
$source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$subview = ArrayView::toView($source)->subview('::2'); // [1, 3, 5, 7, 9]

$data = [9, 27, 45, 63, 81];

$subview->mapWith($data, fn ($lhs, $rhs) => $lhs + $rhs); // [10, 30, 50, 70, 90]
Parameters
$data : array<string|int, U>|ArrayViewInterface<string|int, U>|U

The rhs values for a binary operation.

$mapper : callable
Tags
template

U The type rhs of a binary operation.

throws
ValueError

if the $data is not sequential array.

throws
SizeError

if size of $data not equals to size of the view.

Return values
array<string|int, mixed>

New array with transformed elements of this view.

match()

Checks if all elements in the view satisfy a given predicate function.

public match(callable $predicate) : MaskSelector
Example
$source = [1, 2, 3, 4, 5, 6];
$view = ArrayView::toView($source);

$mask = $view->match(fn ($x) => $x % 2 === 0);
$mask->getValue(); // [false, true, false, true, false, true]

$view->subview($mask)->toArray(); // [2, 4, 6]
$view[$mask]; // [2, 4, 6]

$view[$mask] = [20, 40, 60];
$source; // [1, 20, 3, 40, 5, 60]
Parameters
$predicate : callable
Tags
see
ArrayView::match()

Full synonim.

Return values
MaskSelector

Boolean mask for selecting elements that satisfy the predicate.

matchWith()

Compares the elements of the current ArrayView instance with another array or ArrayView using the provided comparator function.

public matchWith(array<string|int, U>|ArrayViewInterface<string|int, U>|U $data, callable $comparator) : MaskSelectorInterface
Example
$source = [1, 2, 3, 4, 5, 6];
$view = ArrayView::toView($source);

$data = [6, 5, 4, 3, 2, 1];

$mask = $view->matchWith($data, fn ($lhs, $rhs) => $lhs > $rhs);
$mask->getValue(); // [false, false, false, true, true, true]

$view->subview($mask)->toArray(); // [4, 5, 6]
$view[$mask]; // [4, 5, 6]

$view[$mask] = [40, 50, 60];
$source; // [1, 2, 3, 40, 50, 60]
Parameters
$data : array<string|int, U>|ArrayViewInterface<string|int, U>|U

The array or ArrayView to compare to.

$comparator : callable
Tags
template

U The type of the elements in the array for comparison with.

throws
ValueError

if the $data is not sequential array.

throws
SizeError

if size of $data not equals to size of the view.

see
ArrayView::is()

Full synonim.

Return values
MaskSelectorInterface

A MaskSelector instance representing the results of the element comparisons.

offsetExists()

Check if the specified offset exists in the ArrayView object.

public offsetExists((numeric)|S $offset) : bool
$source = [1, 2, 3, 4, 5];
$view = ArrayView::toView($source);

isset($view[0]); // true
isset($view[-1]); // true
isset($view[10]); // false

isset($view[new SliceSelector('::2')]); // true
isset($view[new IndexListSelector([0, 2, 4])]); // true
isset($view[new IndexListSelector([0, 2, 10])]); // false
isset($view[new MaskSelector([true, true, false, false, true])]); // true
isset($view[new MaskSelector([true, true, false, false, true, true])]); // false

isset($view['::2']); // true
isset($view[[0, 2, 4]]); // true
isset($view[[0, 2, 10]]); // false
isset($view[[true, true, false, false, true]]); // true
isset($view[[true, true, false, false, true, true]]); // false
Parameters
$offset : (numeric)|S

The offset to check.

Return values
bool

offsetGet()

Get the value at the specified offset in the ArrayView object.

public offsetGet((numeric)|S $offset) : T|array<string|int, T>
$source = [1, 2, 3, 4, 5];
$view = ArrayView::toView($source);

$view[0]; // 1
$view[-1]; // 5

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

$view['::2']; // [1, 3, 5]
$view[[0, 2, 4]]; // [1, 3, 5]
$view[[true, true, false, false, true]]; // [1, 2, 5]
Parameters
$offset : (numeric)|S

The offset to get the value at.

Tags
throws
IndexError

if the offset is out of range.

throws
KeyError

if the key is invalid.

Return values
T|array<string|int, T>

The value at the specified offset.

offsetSet()

Set the value at the specified offset in the ArrayView object.

public offsetSet((numeric)|S $offset, T|array<string|int, T>|ArrayViewInterface<string|int, T$value) : void
$source = [1, 2, 3, 4, 5];
$view = ArrayView::toView($source);

$view[0] = 11;
$view[-1] = 55;

$source; // [11, 2, 3, 4, 55]

$source = [1, 2, 3, 4, 5];
$view = ArrayView::toView($source);

$view[new SliceSelector('::2')] = [11, 33, 55];
$source; // [11, 2, 33, 4, 55]

$view[new IndexListSelector([1, 3])] = [22, 44];
$source; // [11, 22, 33, 44, 55]

$view[new MaskSelector([true, false, false, false, true])] = [111, 555];
$source; // [111, 22, 33, 44, 555]

$source = [1, 2, 3, 4, 5];
$view = ArrayView::toView($source);

$view['::2'] = [11, 33, 55];
$source; // [11, 2, 33, 4, 55]

$view[[1, 3]] = [22, 44];
$source; // [11, 22, 33, 44, 55]

$view[[true, false, false, false, true]] = [111, 555];
$source; // [111, 22, 33, 44, 555]
Parameters
$offset : (numeric)|S

The offset to set the value at.

$value : T|array<string|int, T>|ArrayViewInterface<string|int, T>

The value to set.

Tags
throws
IndexError

if the offset is out of range.

throws
KeyError

if the key is invalid.

throws
ReadonlyError

if the object is readonly.

offsetUnset()

Unset the value at the specified offset in the array-like object.

public offsetUnset((numeric)|S $offset) : void
Parameters
$offset : (numeric)|S

The offset to unset the value at.

Tags
throws
NotSupportedError

always.

set()

Sets new values for the elements in the view.

public set(array<string|int, T>|ArrayViewInterface<string|int, T>|T $newValues) : ArrayView<string|int, T>
Example
$source = [1, 2, 3, 4, 5];
$subview = ArrayView::toView($source)->subview('::2'); // [1, 3, 5]

$subview->set([11, 33, 55]);
$subview->toArray(); // [11, 33, 55]

$source; // [11, 2, 33, 4, 55]
Parameters
$newValues : array<string|int, T>|ArrayViewInterface<string|int, T>|T

The new values to set.

Tags
throws
ValueError

if the $newValues is not sequential array.

throws
SizeError

if size of $newValues not equals to size of the view.

Return values
ArrayView<string|int, T>

this view.

subview()

Returns a subview of this view based on a selector or string slice.

public subview(S $selector[, bool|null $readonly = null ]) : ArrayViewInterface<string|int, T>
Example (using selector objects)
$source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

$subview = ArrayView::toView($source)
    ->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($source); // [1, 2, 3, 4, 55, 6, 77, 8, 9, 10]
Example (using short objects)
$source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

$subview = ArrayView::toView($source)
    ->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($source); // [1, 2, 3, 4, 55, 6, 77, 8, 9, 10]
Readonly example
$source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$subview = ArrayView::toView($source)->subview('::2');

$subview[':']; // [1, 3, 5, 7, 9]
$subview[':'] = [11, 33, 55, 77, 99]; // throws ReadonlyError
$subview[0] = [11]; // throws ReadonlyError
Parameters
$selector : S

The selector or string to filter the subview.

$readonly : bool|null = null

Flag indicating if the subview should be read-only.

Tags
template

S of string|array<int|bool>|ArrayViewInterface<int|bool>|ArraySelectorInterface Selector type.

throws
IndexError

if the selector is IndexListSelector and some indexes are out of range.

throws
SizeError

if the selector is MaskSelector and size of the mask not equals to size of the view.

throws
KeyError

if the selector is not valid (e.g. non-sequential array).

Return values
ArrayViewInterface<string|int, T>

A new view representing the subview of this view.

toArray()

Returns the array representation of the view.

public toArray() : array<string|int, T>
Example
$source = [1, 2, 3, 4, 5];
$view = ArrayView::toView($source);
$view->toArray(); // [1, 2, 3, 4, 5]
Return values
array<string|int, T>

The array representation of the view.

toUnlinkedView()

Creates an unlinked from source ArrayView instance from the given source array or ArrayView.

public static toUnlinkedView(mixed $source[, bool|null $readonly = null ]) : ArrayViewInterface<string|int, T>
Example:
$source = [1, 2, 3, 4, 5];
$view = ArrayView::toUnlinkedView($source);

$view[0]; // 1
$view['1::2']; // [2, 4]
$view['1::2'] = [22, 44];

$view->toArray(); // [1, 22, 3, 44, 5]
$source; // [1, 2, 3, 4, 5]
Readonly example:
$source = [1, 2, 3, 4, 5];
$view = ArrayView::toUnlinkedView($source, true);

$view['1::2']; // [2, 4]
$view['1::2'] = [22, 44]; // throws ReadonlyError
$view[0] = 11; // throws ReadonlyError
Parameters
$source : mixed

The source array or ArrayView to create a view from.

$readonly : bool|null = null

Optional flag to indicate whether the view should be readonly.

Return values
ArrayViewInterface<string|int, T>

An ArrayView instance based on the source array or ArrayView.

toView()

Creates an ArrayView instance from the given source array or ArrayView.

public static toView(array<string|int, T>|ArrayViewInterface<string|int, T&$source[, bool|null $readonly = null ]) : ArrayViewInterface<string|int, T>
  • If the source is not an ArrayView, a new ArrayView is created with the provided source.
  • If the source is an ArrayView and the readonly parameter is specified as true, a new readonly ArrayView is created.
  • If the source is an ArrayView and it is already readonly, the same ArrayView is returned.
Example
$source = [1, 2, 3, 4, 5];
$view = ArrayView::toView($source);

$view[0]; // 1
$view['1::2']; // [2, 4]
$view['1::2'] = [22, 44];

$view->toArray(); // [1, 22, 3, 44, 5]
$source; // [1, 22, 3, 44, 5]
Readonly example
$source = [1, 2, 3, 4, 5];
$view = ArrayView::toView($source, true);

$view['1::2']; // [2, 4]
$view['1::2'] = [22, 44]; // throws ReadonlyError
$view[0] = 11; // throws ReadonlyError
Parameters
$source : array<string|int, T>|ArrayViewInterface<string|int, T>

The source array or ArrayView to create a view from.

$readonly : bool|null = null

Optional flag to indicate whether the view should be readonly.

Tags
throws
ValueError

if the array is not sequential.

throws
ReadonlyError

if the source is readonly and trying to create a non-readonly view.

Return values
ArrayViewInterface<string|int, T>

An ArrayView instance based on the source array or ArrayView.


        
On this page

Search results