WireArray class

WireArray is the base iterable array type used throughout the ProcessWire framework.

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?        

Traversal

NameReturnSummary 
and($item)
WireArrayReturn a new copy of this WireArray with the given item(s) appended
each()
array null string WireArrayPerform an action upon each item in the WireArray
first()
Wire mixed boolReturns the first item in the WireArray or boolean false if empty.
Can also be used as property: first
 
getIterator()
ArrayObject WireAllows iteration of the WireArray. 
last()
Wire mixed boolReturns the last item in the WireArray or boolean false if empty.
Can also be used as property: last
 
slices(int $qty)
arrayDivide this WireArray into $qty slices and return array of them (each being another WireArray) 

Retrieval

NameReturnSummary 
count()
intReturns the number of items in this WireArray.
Can also be used as property: count
 
eq(int $num)
Wire nullReturns the item at the given index starting from 0, or NULL if it doesn't exist. 
explode()
arrayReturn a plain array of the requested property from each item 
find($selector)
WireArrayFind all items in this WireArray that match the given selector. 
findOne($selector)
Wire boolFind a single item by selector 
findRandom(int $num)
WireArrayFind a specified quantity of random elements from this WireArray. 
findRandomTimed(int $num)
WireArrayFind a quantity of random elements from this WireArray based on a timed interval (or user provided seed). 
first()
Wire mixed boolReturns the first item in the WireArray or boolean false if empty.
Can also be used as property: first
 
get($key)
WireData Page mixed array nullReturns the value of the item at the given index, or null if not set. 
getAll()
$thisReturns all items in the WireArray (for syntax convenience) 
getArray()
arrayGet a PHP array of all the items in this WireArray with original keys maintained 
getKeys()
arrayReturns a regular PHP array of all keys used in this WireArray. 
getNext(Wire $item)
Wire nullGiven an item, get the item that comes after it in the WireArray 
getPrev(Wire $item)
Wire nullGiven an item, get the item before it in the WireArray 
getProperty(string $property)
Wire mixedGet a predefined property of the array, or extra data that has been set. 
getRandom()
WireArray Wire mixed nullGet a random item from this WireArray. 
getValues()
array WireReturns a regular PHP array of all values used in this WireArray. 
has($key)
boolDoes this WireArray have the given item, index, or match the given selector? 
implode(string $delimiter)
stringCombine all elements into a delimiter-separated string containing the given property from each item 
index(int $num)
WireArrayReturns a new WireArray of the item at the given index. 
last()
Wire mixed boolReturns the last item in the WireArray or boolean false if empty.
Can also be used as property: last
 
pop()
Wire mixed nullPop an element off the end of the WireArray and return it 
reverse()
WireArrayReturn a new reversed version of this WireArray. 
shift()
Wire mixed nullShift an element off the beginning of the WireArray and return it 
slice(int $start)
WireArrayGet a slice of the WireArray. 
slices(int $qty)
arrayDivide this WireArray into $qty slices and return array of them (each being another WireArray) 
unique()
WireArrayReturn a new array that is unique (no two of the same elements) 

Manipulation

NameReturnSummary 
add($item)
$thisAdd an item to the end of the WireArray. 
append($item)
$thisAppend an item to the end of the WireArray 
filter($selector)
$thisFilter this WireArray to only include items that match the given selector (destructive) 
import($items)
$thisImport the given item(s) into this WireArray. 
insertAfter($item, $existingItem)
$thisInsert an item after an existing item 
insertBefore($item, $existingItem)
$thisInsert an item before an existing item 
not($selector)
$thisFilter this WireArray to only include items that DO NOT match the selector (destructive) 
pop()
Wire mixed nullPop an element off the end of the WireArray and return it 
prepend($item)
$thisPrepend an item to the beginning of the WireArray. 
push($item)
$thisPush an item to the end of the WireArray. 
remove($key)
$thisRemoves the given item or index from the WireArray (if it exists). 
removeAll()
$thisRemoves all items from the WireArray, leaving it blank 
removeItems($items)
$thisRemoves multiple identified items at once 
replace($itemA, $itemB)
$thisReplace one item with the other 
set($key, $value)
$thisSet an item by key in the WireArray. 
setArray($data)
$thisLike set() but accepts an array or WireArray to set multiple values at once 
shift()
Wire mixed nullShift an element off the beginning of the WireArray and return it 
shuffle()
$thisShuffle/randomize this WireArray 
sort($properties)
$thisSort this WireArray by the given properties. 
sortFlags()
intGet or set sort flags that affect behavior of any sorting functions 
unshift($item)
$thisUnshift an element to the beginning of the WireArray (alias for prepend) 

Info

NameReturnSummary 
has($key)
boolDoes this WireArray have the given item, index, or match the given selector? 
isIdentical(WireArray $items)
boolIs the given WireArray identical to this one? 
isValidItem(mixed $item)
boolIs the given item valid for storange in this array? 
isValidKey($key)
boolIs the given item key valid for use in this array? 
iterable(mixed $item)
boolDetermines if the given item iterable as an array. 

Output rendering

NameReturnSummary 
each()
array null string WireArrayPerform an action upon each item in the WireArray
implode(string $delimiter)
stringCombine all elements into a delimiter-separated string containing the given property from each item 

Other data storage

NameReturnSummary 
data()
WireArray mixed array nullStore or retrieve an extra data value in this WireArray 
removeData(string $key)
$thisRemove a property/value previously set with the data() method. 

Changes

NameReturnSummary 
getItemsAdded()
array WireReturn array of all items added to this WireArray (while change tracking is enabled) 
getItemsRemoved()
array WireReturn array of all items removed from this WireArray (when change tracking is enabled) 

Fun tools

NameReturnSummary 
and($item)
WireArrayReturn a new copy of this WireArray with the given item(s) appended
callUnknown(string $method, array $arguments)
null mixedHandler for when an unknown/unhooked method call is executed
each()
array null string WireArrayPerform an action upon each item in the WireArray
explode()
arrayReturn a plain array of the requested property from each item 
implode(string $delimiter)
stringCombine all elements into a delimiter-separated string containing the given property from each item 

For hooks

These methods are only useful for hooking and should not be called directly.

Common

NameReturnSummary 
WireArray new()
static)
keys array All keys used in this WireArray 
setDuplicateChecking(bool $value)
(nothing)Set the current duplicate checking state 
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.244

“Indeed, if ProcessWire can be considered as a CMS in its own right, it also offers all the advantages of a CMF (Content Management Framework). Unlike other solutions, the programmer is not forced to follow the proposed model and can integrate his/her ways of doing things.” —Guy Verville, Spiria Digital Inc.