ArrayViewAccessTrait
Trait providing methods for accessing elements in ArrayView object.
The trait implements methods for accessing, retrieving, setting, and unsetting elements in the ArrayView object.
Tags
Table of Contents
Methods
- 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.
Methods
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
Attributes
- #[ReturnTypeWillChange]
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
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.