MemoizableArray

Type
Class
Namespace
craft\base
Inherits
craft\base\MemoizableArray
Implements
Countable (opens new window), IteratorAggregate (opens new window)
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:

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

View source (opens new window)

# Public Methods

Method Description
__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 (opens new window)

Arguments

# all()

Returns all items.

View source (opens new window)

Returns

array (opens new window)

# count()

View source (opens new window)

Returns

integer (opens new window)

# 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 (opens new window)

Arguments

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

Returns

\craft\base\T, null (opens new window) – The first matching value, or null if no match is found

# getIterator()

View source (opens new window)

Returns

ArrayIterator (opens new window)

# 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 (opens new window)

Arguments

  • $key (string (opens new window)) – The column name whose result will be used to index the array
  • $value (mixed) – The value that $key should be compared with
  • $strict (boolean (opens new window)) – 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 (opens new window)

Arguments

Returns

self – The filtered array