Array Processing – Nette\Utils\Arrays

Nette\Utils\Arrays is a static class, which contains a handful of handy array functions.

All examples assume the following class alias is defined:

use Nette\Utils\Arrays;

get($array, $key, $default=NULL)

Returns $array[$key] item. If it does not exist, Nette\InvalidArgumentException is thrown, unless a default return value is set as third argument.

// if $array['foo'] does not exist, throws an exception
$value = Arrays::get($array, 'foo');

// if $array['foo'] does not exist, returns 'bar'
$value = Arrays::get($array, 'foo', 'bar');

Argument $key may as well be an array.

$array = array('color' => array('favorite' => 'red'), 5);

$value = Arrays::get($array, array('color', 'favorite'));
// returns 'red'

getRef(&$array, $key)

Gets reference to given $array[$key]. If the index does not exist, new one is created with NULL.

$valueRef = & Arrays::getRef($array, 'foo');
// returns $array['foo'] reference

Works with multidimensional arrays as well as get().

$value = & Arrays::get($array, array('color', 'favorite'));
// returns $array['color']['favorite'] reference

grep($array, $pattern, $flags=NULL)

Returns only those array items, which matches a regular expression $pattern. Regex compilation or runtime error throw Nette\RegexpException.

$filteredArray = Arrays::grep($array, '~^\d+$~');
// returns only numerical items

Value PREG_GREP_INVERT may be set as $flags, which inverts the selection.

searchKey($array, $key)

Returns zero-indexed position of given array key. Returns FALSE if key is not found.

$array = array('first' => 10, 'second' => 20);
$position = Arrays::searchKey($array, 'first'); // returns 0

insertAfter(&$array, $key, $inserted)

Appends array $inserted after item with $key index. If such a $key does not exist, the array is inserted at the end.

$array = array('first' => 10, 'second' => 20);
Arrays::insertAfter($array, 'first', array('hello' => 'world'));
// $array = array('first' => 10, 'hello' => 'world', 'second' => 20);

insertBefore(&$array, $key, $inserted)

Prepends content of $inserted array into $array before item with $key index. If such a $key does not exist, the array is inserted at the beginning.

$array = array('first' => 10, 'second' => 20);
Arrays::insertBefore($array, 'first', array('hello' => 'world'));
// $array = array('hello' => 'world', 'first' => 10, 'second' => 20);

mergeTree($array1, $array2)

Merges two arrays recursively. Useful for combining tree structures. It behaves as the + operator applied to arrays, ie. it adds to keys/values of the second array to the the first one. In case of collision, values of first array are used.

$array1 = array('color' => array('favorite' => 'red'), 5);
$array2 = array(10, 'color' => array('favorite' => 'green', 'blue'));

$array = Arrays::mergeTree($array1, $array2);
// $array = array('color' => array('favorite' => 'red', 'blue'), 5);

Values from second array are always appended to the first. Though the disappearance of value 10 might be confusing, it's fine – both 5 of the first array and the 10 do have same key 0.

renameKey(&$array, $oldKey, $newKey)

Renames a key.

$array = array('first' => 10, 'second' => 20);
Arrays::renameKey($array, 'first', 'renamed');
// $array = array('renamed' => 10, 'second' => 20);