class Arrays

Array tools library.

Traits

Static class.

Methods

static mixed
get(array $arr, $key, $default = null)

Returns item from array or $default if item is not set.

static mixed
getRef(array $arr, $key)

Returns reference to array item.

static array
mergeTree(array $arr1, array $arr2)

Recursively appends elements of remaining keys from the second array to the first.

static int|null
searchKey(array $arr, $key)

Searches the array for a given key and returns the offset if successful.

static void
insertBefore(array $arr, $key, array $inserted)

Inserts new array before item specified by key.

static void
insertAfter(array $arr, $key, array $inserted)

Inserts new array after item specified by key.

static void
renameKey(array $arr, $oldKey, $newKey)

Renames key in array.

static array
grep(array $arr, string $pattern, int $flags = 0)

Returns array entries that match the pattern.

static array
flatten(array $arr, bool $preserveKeys = false)

Returns flattened array.

static bool
isList($value)

Finds whether a variable is a zero-based integer indexed array.

static array|stdClass
associate(array $arr, $path)

Reformats table to associative tree. Path looks like 'field|field[]field->field=field'.

static array
normalize(array $arr, $filling = null)

Normalizes to associative array.

static mixed
pick(array $arr, $key, $default = null)

Picks element from the array by key and return its value.

static bool
some(array $arr, callable $callback)

Tests whether some element in the array passes the callback test.

static bool
every(array $arr, callable $callback)

Tests whether all elements in the array pass the callback test.

static array
map(array $arr, callable $callback)

Applies the callback to the elements of the array.

static object
toObject(array $arr, $obj)

Converts array to object

Details

at line 29
static mixed get(array $arr, $key, $default = null)

Returns item from array or $default if item is not set.

Parameters

array $arr
$key
$default

Return Value

mixed

Exceptions

InvalidArgumentException if item does not exist and default value is not provided

at line 51
static mixed getRef(array $arr, $key)

Returns reference to array item.

Parameters

array $arr
$key

Return Value

mixed

Exceptions

InvalidArgumentException if traversed item is not an array

at line 67
static array mergeTree(array $arr1, array $arr2)

Recursively appends elements of remaining keys from the second array to the first.

Parameters

array $arr1
array $arr2

Return Value

array

at line 83
static int|null searchKey(array $arr, $key)

Searches the array for a given key and returns the offset if successful.

Parameters

array $arr
$key

Return Value

int|null offset if it is found, null otherwise

at line 93
static void insertBefore(array $arr, $key, array $inserted)

Inserts new array before item specified by key.

Parameters

array $arr
$key
array $inserted

Return Value

void

at line 103
static void insertAfter(array $arr, $key, array $inserted)

Inserts new array after item specified by key.

Parameters

array $arr
$key
array $inserted

Return Value

void

at line 114
static void renameKey(array $arr, $oldKey, $newKey)

Renames key in array.

Parameters

array $arr
$oldKey
$newKey

Return Value

void

at line 128
static array grep(array $arr, string $pattern, int $flags = 0)

Returns array entries that match the pattern.

Parameters

array $arr
string $pattern
int $flags

Return Value

array

at line 137
static array flatten(array $arr, bool $preserveKeys = false)

Returns flattened array.

Parameters

array $arr
bool $preserveKeys

Return Value

array

at line 151
static bool isList($value)

Finds whether a variable is a zero-based integer indexed array.

Parameters

$value

Return Value

bool

at line 161
static array|stdClass associate(array $arr, $path)

Reformats table to associative tree. Path looks like 'field|field[]field->field=field'.

Parameters

array $arr
$path

Return Value

array|stdClass

at line 212
static array normalize(array $arr, $filling = null)

Normalizes to associative array.

Parameters

array $arr
$filling

Return Value

array

at line 228
static mixed pick(array $arr, $key, $default = null)

Picks element from the array by key and return its value.

Parameters

array $arr
$key
$default

Return Value

mixed

Exceptions

InvalidArgumentException if item does not exist and default value is not provided

at line 247
static bool some(array $arr, callable $callback)

Tests whether some element in the array passes the callback test.

Parameters

array $arr
callable $callback

Return Value

bool

at line 261
static bool every(array $arr, callable $callback)

Tests whether all elements in the array pass the callback test.

Parameters

array $arr
callable $callback

Return Value

bool

at line 275
static array map(array $arr, callable $callback)

Applies the callback to the elements of the array.

Parameters

array $arr
callable $callback

Return Value

array

at line 290
static object toObject(array $arr, $obj)

Converts array to object

Parameters

array $arr
$obj

Return Value

object