Skip to content

MemoizableArray ​

Type
Class
Namespace
craft\base
Inherits
craft\base\MemoizableArray
Implements
Countable, IteratorAggregate
Since
3.5.8

MemoizableArray represents an array of values that need to be run through craft\helpers\ArrayHelper::where() or craft\helpers\ArrayHelper::firstWhere() repeatedly, where it could be beneficial if the results were memoized.

Any class properties that are set to an instance of this class should be excluded from class serialization:

php
public function __serialize()
{
    $vars = get_object_vars($this);
    unset($vars['myMemoizedPropertyName'];
    return $vars;
}

View source

Public Methods ​

MethodDescription
__construct()Constructor
all()Returns all items.
count()
firstWhere()Returns the first value where a given key (the name of a sub-array key or sub-object property) is set to a given value.
getIterator()
where()Filters the array to only the values where a given key (the name of a sub-array key or sub-object property) is set to a given value.
whereIn()Filters the array to only the values where a given key (the name of a sub-array key or sub-object property) is set to one of a given range of values.

__construct() ​

Constructor

View source

Arguments ​

  • $elements (array) – The items to be memoized
  • $normalizer (callable, null) – A method that the items should be normalized with when first returned by all() or firstWhere().

all() ​

Returns all items.

View source

Returns ​

array

count() ​

View source

Returns ​

integer

firstWhere() ​

Returns the first value where a given key (the name of a sub-array key or sub-object property) is set to a given value.

View source

Arguments ​

  • $key (string) – The column name whose result will be used to index the array
  • $value (mixed) – The value that $key should be compared with
  • $strict (boolean) – Whether a strict type comparison should be used when checking array element values against $value

Returns ​

\craft\base\T, null – The first matching value, or null if no match is found

getIterator() ​

View source

Returns ​

ArrayIterator

where() ​

Filters the array to only the values where a given key (the name of a sub-array key or sub-object property) is set to a given value.

Array keys are preserved by default.

View source

Arguments ​

  • $key (string) – The column name whose result will be used to index the array
  • $value (mixed) – The value that $key should be compared with
  • $strict (boolean) – Whether a strict type comparison should be used when checking array element values against $value

Returns ​

self – The filtered array

whereIn() ​

Filters the array to only the values where a given key (the name of a sub-array key or sub-object property) is set to one of a given range of values.

Array keys are preserved by default.

View source

Arguments ​

  • $key (string) – The column name whose result will be used to index the array
  • $values (array) – The value that $key should be compared with
  • $strict (boolean) – Whether a strict type comparison should be used when checking array element values against $values

Returns ​

self – The filtered array