Documentation

ArrayViewOperationsTrait

Trait providing methods for operation methods of ArrayView.

Tags
template

T Type of ArrayView values.

template

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

Table of Contents

Methods

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.
filter()  : ArrayMaskView<string|int, T>
Filters the elements in the view based on a predicate function.
is()  : MaskSelector
Checks if all elements in the view satisfy a given predicate function.
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.

Methods

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.

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.

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.

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.


        
On this page

Search results