Skip to content Skip to footer navigation

Key By Modifier

Re-keys a numerically indexed array using the value of a given key on each item. Handy when you want to target specific items by name instead of looping, like the fields array inside a form tag.

fields:
-
handle: name
display: 'Your Name'
-
handle: email
display: 'Email Address'
{{ rekeyed = fields | key_by('handle') }}
<div class="name-field">{{ rekeyed:name:display }}</div> }}</div>
<div class="email-field">{{ rekeyed:email:display }}</div> }}</div>
@php $rekeyed = Statamic::modify($fields)->keyBy('handle')->fetch(); @endphp
<div class="name-field">{{ $rekeyed['name']['display'] }}</div>
<div class="email-field">{{ $rekeyed['email']['display'] }}</div>
<div class="name-field">Your Name</div>
<div class="email-field">Email Address</div>
Hot Tip!

If two items share the same key, the last one wins. Pick a key that's unique across the array (like handle or id).

A troll pointing a teaching stick