WireArray

The base collection class for all ProcessWire iterable sets

Nearly every collection you encounter — pages, fields, templates, modules, roles, and more — is a WireArray subclass. Its API is jQuery-inspired: most mutation methods return $this for fluent chaining, and both foreach and array access work out of the box.

WireArray is not itself an API variable. You encounter it through the collections returned by the API: $page->children(), $pages->find(), $modules, and many more — all of these are WireArray subclasses.

Adding items
// Append to end — all three are equivalent
$items->add($item);
$items->append($item);
$items[] = $item;           // array access

// Prepend to beginning
$items->prepend($item);

// Insert relative to an existing item
$items->insertBefore($newItem, $existingItem);
$items->insertAfter($newItem, $existingItem);

// Import multiple items (array or WireArray)
$items->import($otherArray);

// Swap two items, or replace one with the other
$items->replace($itemA, $itemB);
Retrieving items

get($key)

The primary retrieval method. Accepts many forms:

// By array key
$item = $items->get('template-name'); // by name key
$item = $items->get(1234);            // by numeric key

// By numeric position (0-based), negative counts from end
$item = $items->eq(0);   // first item
$item = $items->eq(-1);  // last item

// First and last
$first = $items->first();
$last  = $items->last();

// First item matching a selector
$item = $items->get("name=foo-bar");

// Pipe fallback — returns first item found by key/name
$module = $modules->get("MarkupPageArray|MarkupAdminDataTable");

// Get an array of values from each item via "property[]" syntax
$titles = $items->get("title[]"); // returns plain PHP array

// Populate a template string with item properties
$html = $items->get("Total: {count}, First: {first}");

// Get multiple items by passing an array of keys
$subset = $items->get([1, 2, 3]); // returns associative array

getArray(), getValues(), getKeys()

$assoc   = $items->getArray();  // PHP array, original keys preserved
$values  = $items->getValues(); // PHP array, re-indexed from 0
$keys    = $items->getKeys();   // PHP array of keys only

slice($start, $limit) and eq($n)

$first3 = $items->slice(0, 3);  // returns new WireArray
$last3  = $items->slice(-3);

$third  = $items->eq(2);        // returns single item (0-based)
$last   = $items->eq(-1);       // last item

Random selection

$one   = $items->getRandom();      // single random item
$three = $items->getRandom(3);     // new WireArray of 3 random items
$three = $items->findRandom(3);    // always returns WireArray

// Seeded random — same result for the same day (useful for rotating featured items)
$three = $items->findRandomTimed(3);         // daily seed (default)
$three = $items->findRandomTimed(3, 'YmdH'); // hourly seed
$three = $items->findRandomTimed(3, 42);     // fixed integer seed
Checking and counting
// Count — both forms work
$n = $items->count();
$n = count($items);
$n = $items->count; // property

// Check if item, key, or selector matches anything
if($items->has($item))           { /* item object is present */ }
if($items->has('foo-bar'))       { /* item with name 'foo-bar' is present */ }
if($items->has("status>0"))      { /* at least one item matches selector */ }

// Check identity
if($items->isIdentical($other))      { /* same items, order, and data */ }
if($items->isIdentical($other, false)) { /* same items and order only */ }
Finding items (non-destructive)

These return a new WireArray and leave the original unchanged.

// All items matching a selector
$matches = $items->find("featured=1");

// First item matching a selector (returns item or false)
$match = $items->findOne("name=foo");
$match = $items->findOne("status>0, sort=-created");

// Reversed copy
$reversed = $items->reverse();

// Unique items only
$unique = $items->unique();

// Get adjacent items
$next = $items->getNext($item);
$prev = $items->getPrev($item);

// Divide into N equal slices (returns PHP array of WireArray objects)
$slices = $items->slices(3);
Filtering items (destructive)

These modify the WireArray in place. Use find() instead if you need a copy.

// Keep only items matching a selector
$items->filter("featured=1");

// Remove items matching a selector
$items->not("status=0");

// Sort by one or more properties (CSV or array)
$items->sort("last_name, first_name");
$items->sort("-created");     // descending
$items->sort("sort, title");  // by multiple fields

// Randomise order in place
$items->shuffle();

Sort supports dot-notation for sub-properties (sort("parent.title")), and the special value "random" as an alias for shuffle().

Removing items
// Remove by item object, key, or selector
$items->remove($item);
$items->remove('foo-bar');          // by name key
$items->remove("status=0");         // by selector 3.0.196+

// Remove multiple
$items->removeItems([$itemA, $itemB]);

// Remove all
$items->removeAll();

// Pop/shift (remove and return)
$last  = $items->pop();   // removes and returns last item
$first = $items->shift();  // removes and returns first item
Iterating and rendering

foreach

foreach($items as $item) {
    echo $item->title;
}

foreach($items as $key => $item) {
    echo "$key: $item->title\n";
}

each()

The most versatile iteration method. Accepts a callable, a template string, a property name, or an array of property names.

// Callable — return strings to build a concatenated result
echo $items->each(function($item) {
    return "<li>$item->title</li>";
});

// With key as first argument when function has 2 parameters
echo $items->each(function($key, $item) {
    return "<li>$key: $item->title</li>";
});

// Template string with {tags}
echo $items->each("<li><a href='{url}'>{title}</a></li>");

// Property name — returns PHP array of values
$titles = $items->each("title");

// Multiple properties — returns array of associative arrays
$data = $items->each(["title", "url"]);

implode() — build a delimited string

// Comma-separated list of titles
echo $items->implode(", ", "title");

// Template string with {tags}
echo $items->implode(' / ', '<a href="{url}">{title}</a>');

// Custom function per item
echo $items->implode(", ", function($item) {
    return "$item->title ($item->id)";
});

// With options
echo $items->implode(", ", "title", ['skipEmpty' => true, 'prepend' => 'Items: ']);

explode() — extract a property as a PHP array

$titles = $items->explode("title");    // ['Title A', 'Title B', ...]

// Multiple properties: [['title' => 'Title A', 'url' => '/foo/'], ...]
$data = $items->explode(["title", "url"]);

// With a custom key: [ 'foo-bar' => 'Foo Bar', ...]
$byName = $items->explode("title", ['key' => 'name']); // keyed by 'name' property

Magic property dispatch

Calling an unknown method on a WireArray automatically delegates to explode() or implode() for a property having the name you specified as a method:

// same as $items->explode('title'): [ 'Foo', 'Bar', 'Baz', ...]
$titles = $items->title();

// same as $items->implode(", ", "title"): "Foo, Bar, Baz"
$str = $items->title(", ");
Extra data storage

WireArray carries a separate $extraData store (not the items themselves) for arbitrary metadata you want to attach to the collection — similar to jQuery's .data():

// Set
$items->data('totalCount', 500);
$items->data(['page' => 2, 'limit' => 10]);

// Get
$total = $items->data('totalCount');
$all   = $items->data();

// Remove
$items->removeData('totalCount');
$items->data(false, 'totalCount'); // alternative unset form

// Replace all extra data
$items->data(['page' => 3], true);
Creating new instances
// Global shortcut function — most concise form
$a = WireArray();              // empty
$a = WireArray('foo');         // one item
$a = WireArray(['foo', 'bar']); // from PHP array

// Static factory — works on any WireArray subclass (PHP 7+)
$a = WireArray::new();             // empty
$a = WireArray::new($item);        // one item
$a = WireArray::new([$a, $b]);     // from PHP array
$a = WireArray::new($otherWireArray);

// Blank instance of the same concrete type (useful in subclasses)
$copy = $items->makeNew();

// Full clone (same type, same items)
$copy = clone $items;
$copy = $items->makeCopy(); // equivalent

WireData() and PageArray() are analogous shortcut functions for those types (also in wire/core/Functions.php).

Change tracking

WireArray tracks which items were added or removed while change tracking is on, in addition to the property-level change tracking inherited from Wire.

$items->setTrackChanges(true);

$items->add($newItem);
$items->remove($oldItem);

$added   = $items->getItemsAdded();   // array of added items
$removed = $items->getItemsRemoved(); // array of removed items

$items->resetTrackChanges(); // clears added/removed lists and disables tracking
Notes
  • Source file: wire/core/WireArray/WireArray.php
  • Extends: Wire — all hook and API-variable access from Wire is available
  • Implements: IteratorAggregate, ArrayAccess, Countable
  • Common subclasses: PageArray, Modules, Fieldtypes, and many module-defined collection types. Note that Fields, Templates, Fieldgroups, Roles, Users, and Permissions are iterable Wire objects but extend WireSaveableItems or PagesType, not WireArray directly.
  • Destructive vs non-destructive: filter(), not(), and sort() modify the collection in place; find(), findOne(), reverse(), unique(), and slice() return new collections
  • Duplicate checking: subclasses that use named keys prevent duplicate items automatically; plain WireArray instances do not, unless you call setDuplicateChecking(true).
  • get() vs array access: get($key) returns null for missing items; array access $items[$key] returns false
  • each() return value: returns $this when the callback returns nothing (for chaining), or a concatenated string when callbacks return strings
  • sort() special values: prepend hyphen (-) to any property name for descending order ("-created"); use "random" as the sole property to shuffle
  • String representation: casting a WireArray to string returns its items joined by | (using each item's own __toString())
API reference: methods, properties, hooks

Nearly all collections of items in ProcessWire are derived from the WireArray type. This includes collections of pages, fields, templates, modules and more. As a result, the WireArray class is one you will be interacting with regularly in the ProcessWire API, whether you know it or not.

Below are all the public methods you can use to interact with WireArray types in ProcessWire. In addition to these methods, you can also treat WireArray types like regular PHP arrays, in that you can foreach() them and get or set elements using array syntax, i.e. $value = $items[$key]; to get an item or $items[] = $item; to add an item.


Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the WireArray class also inherits all the methods and properties of: Wire.

Show class?     Show args?       Only hookable?    

Traversal

NameReturnSummary 
WireArray::and($item)
WireArray

Return a new copy of this WireArray with the given item(s) appended

WireArray::each()
array null string WireArray

Perform an action upon each item in the WireArray

 
WireArray::first()
Wire mixed bool

Returns the first item in the WireArray or boolean false if empty.


Can also be used as property: WireArray::first
 
WireArray::getIterator()
ArrayObject Wire

Allows iteration of the WireArray.

 
WireArray::last()
Wire mixed bool

Returns the last item in the WireArray or boolean false if empty.


Can also be used as property: WireArray::last
 
WireArray::slices(int $qty)
array

Divide this WireArray into $qty slices and return array of them (each being another WireArray)

 

Retrieval

NameReturnSummary 
WireArray::count()
int

Returns the number of items in this WireArray.


Can also be used as property: WireArray::count
 
WireArray::eq(int $num)
Wire null

Returns the item at the given index starting from 0, or NULL if it doesn't exist.

 
WireArray::explode()
array

Return a plain array of the requested property from each item

 
WireArray::find($selector)
WireArray

Find all items in this WireArray that match the given selector.

 
WireArray::findOne($selector)
Wire bool

Find a single item by selector

 
WireArray::findRandom(int $num)
WireArray

Find a specified quantity of random elements from this WireArray.

 
WireArray::findRandomTimed(int $num)
WireArray

Find a quantity of random elements from this WireArray based on a timed interval (or user provided seed).

 
WireArray::first()
Wire mixed bool

Returns the first item in the WireArray or boolean false if empty.


Can also be used as property: WireArray::first
 
WireArray::get($key)
WireData Page mixed array null

Returns the value of the item at the given index, or null if not set.

 
WireArray::getAll()
$this

Returns all items in the WireArray (for syntax convenience)

 
WireArray::getArray()
array

Get a PHP array of all the items in this WireArray with original keys maintained

 
WireArray::getKeys()
array

Returns a regular PHP array of all keys used in this WireArray.

 
WireArray::getNext(Wire $item)
Wire null

Given an item, get the item that comes after it in the WireArray

 
WireArray::getPrev(Wire $item)
Wire null

Given an item, get the item before it in the WireArray

 
WireArray::getProperty(string $property)
Wire mixed

Get a predefined property of the array, or extra data that has been set.

 
WireArray::getRandom()
WireArray Wire mixed null

Get a random item from this WireArray.

 
WireArray::getValues()
array Wire

Returns a regular PHP array of all values used in this WireArray.

 
WireArray::has($key)
bool

Does this WireArray have the given item, index, or match the given selector?

 
WireArray::implode(string $delimiter)
string

Combine all elements into a delimiter-separated string containing the given property from each item

 
WireArray::index(int $num)
WireArray

Returns a new WireArray of the item at the given index.

 
WireArray::last()
Wire mixed bool

Returns the last item in the WireArray or boolean false if empty.


Can also be used as property: WireArray::last
 
WireArray::pop()
Wire mixed null

Pop an element off the end of the WireArray and return it

 
WireArray::reverse()
WireArray

Return a new reversed version of this WireArray.

 
WireArray::shift()
Wire mixed null

Shift an element off the beginning of the WireArray and return it

 
WireArray::slice(int $start)
WireArray

Get a slice of the WireArray.

 
WireArray::slices(int $qty)
array

Divide this WireArray into $qty slices and return array of them (each being another WireArray)

 
WireArray::unique()
WireArray

Return a new array that is unique (no two of the same elements)

 

Manipulation

NameReturnSummary 
WireArray::add($item)
$this

Add an item to the end of the WireArray.

 
WireArray::append($item)
$this

Append an item to the end of the WireArray

 
WireArray::filter($selector)
$this

Filter this WireArray to only include items that match the given selector (destructive)

 
WireArray::import($items)
$this

Import the given item(s) into this WireArray.

 
WireArray::insertAfter($item, $existingItem)
$this

Insert an item after an existing item

 
WireArray::insertBefore($item, $existingItem)
$this

Insert an item before an existing item

 
WireArray::not($selector)
$this

Filter this WireArray to only include items that DO NOT match the selector (destructive)

 
WireArray::pop()
Wire mixed null

Pop an element off the end of the WireArray and return it

 
WireArray::prepend($item)
$this

Prepend an item to the beginning of the WireArray.

 
WireArray::push($item)
$this

Push an item to the end of the WireArray.

 
WireArray::remove($key)
$this

Removes the given item or index from the WireArray (if it exists).

 
WireArray::removeAll()
$this

Removes all items from the WireArray, leaving it blank

 
WireArray::removeItems($items)
$this

Removes multiple identified items at once

 
WireArray::replace($itemA, $itemB)
$this

Replace one item with the other

 
WireArray::set($key, $value)
$this

Set an item by key in the WireArray.

 
WireArray::setArray($data)
$this

Like set() but accepts an array or WireArray to set multiple values at once

 
WireArray::shift()
Wire mixed null

Shift an element off the beginning of the WireArray and return it

 
WireArray::shuffle()
$this

Shuffle/randomize this WireArray

 
WireArray::sort($properties)
$this

Sort this WireArray by the given properties.

 
WireArray::sortFlags()
int

Get or set sort flags that affect behavior of any sorting functions

 
WireArray::unshift($item)
$this

Unshift an element to the beginning of the WireArray (alias for prepend)

 

Info

NameReturnSummary 
WireArray::has($key)
bool

Does this WireArray have the given item, index, or match the given selector?

 
WireArray::isIdentical(WireArray $items)
bool

Is the given WireArray identical to this one?

 
WireArray::isValidItem(mixed $item)
bool

Is the given item valid for storage in this array?

 
WireArray::isValidKey($key)
bool

Is the given item key valid for use in this array?

 
WireArray::iterable(mixed $item)
bool

Determines if the given item iterable as an array.

 

Output rendering

NameReturnSummary 
WireArray::each()
array null string WireArray

Perform an action upon each item in the WireArray

 
WireArray::implode(string $delimiter)
string

Combine all elements into a delimiter-separated string containing the given property from each item

 

Other data storage

NameReturnSummary 
WireArray::data()
WireArray mixed array null

Store or retrieve an extra data value in this WireArray

 
WireArray::removeData(string $key)
$this

Remove a property/value previously set with the WireArray::data() method.

 

Changes

NameReturnSummary 
WireArray::getItemsAdded()
array Wire

Return array of all items added to this WireArray (while change tracking is enabled)

 
WireArray::getItemsRemoved()
array Wire

Return array of all items removed from this WireArray (when change tracking is enabled)

 

Fun tools

NameReturnSummary 
WireArray::and($item)
WireArray

Return a new copy of this WireArray with the given item(s) appended

WireArray::callUnknown(string $method, array $arguments)
null mixed

Handler for when an unknown/unhooked method call is executed

WireArray::each()
array null string WireArray

Perform an action upon each item in the WireArray

 
WireArray::explode()
array

Return a plain array of the requested property from each item

 
WireArray::implode(string $delimiter)
string

Combine all elements into a delimiter-separated string containing the given property from each item

 

Properties

NameReturnSummary 
WireArray::keys array All keys used in this WireArray 
WireArray::values array All values used in this WireArray 

Additional methods and properties

In addition to the methods and properties above, WireArray also inherits the methods and properties of these classes:

API reference based on ProcessWire core version 3.0.264