Return the element at a given position of an array, using array_slice(). The position is based on the order of the
elements, not on their keys, so it also works with associative arrays. Negative indexes are counted from the end.
- Service:
CleverAge\ProcessBundle\Transformer\Array\ArrayElementTransformer - Transformer code:
array_element
array
any: the element found at the given position, null when the position is outside the array (at either end)
| Code | Type | Required | Default | Description |
|---|---|---|---|---|
index |
int |
X | Position of the element (0-based, a negative value starts from the end) |
- Get the 2nd element:
['foo', 'bar', 'baz']becomes'bar'
# Transformer options level
array_element:
index: 1- Get the penultimate element:
['foo', 'bar', 'baz']becomes'bar'
# Transformer options level
array_element:
index: -2- An index outside the array:
['foo']becomesnull
# Transformer options level
array_element:
index: 3