class Arrays

Array tools library.

Methods

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

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

static mixed
getRef(array $arr, string|int|array $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, string|int $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, object $obj)

Converts array to object

Details

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

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

Parameters

array $arr
string|int|array $key

one or more keys

$default

Return Value

mixed

Exceptions

InvalidArgumentException

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

Returns reference to array item.

Parameters

array $arr
string|int|array $key

one or more keys

Return Value

mixed

Exceptions

InvalidArgumentException

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 215
static array normalize(array $arr, $filling = null)

Normalizes to associative array.

Parameters

array $arr
$filling

Return Value

array

at line 231
static mixed pick(array $arr, string|int $key, $default = null)

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

Parameters

array $arr
string|int $key

array key

$default

Return Value

mixed

Exceptions

InvalidArgumentException

at line 250
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 264
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 278
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 293
static object toObject(array $arr, object $obj)

Converts array to object

Parameters

array $arr
object $obj

Return Value

object

Traits

Static class.