class ArrayUtil (View source)

Various PHP array related utility functions.

Methods

static mixed
findByKeyOrNull(array $array, mixed $key)

Find an item in an associative array by a key value, or return null if not found.

static mixed
findByKeyOrDefault(array $array, mixed $key, mixed $default)

Find an item in an associative array by a key value, or return default if not found.

static 
arrayMergeIgnoreCase()

Merge a number of arrays using a case insensitive comparison for the array keys.

static bool
isAssociative(array $arr)

Checks whether an array's keys are associative. An array's keys are associate if they are not values 0 to count(array) - 1.

static bool
all(array $array, callable $predicate)

Checks whether every value in an array passes the provided predicate.

static bool
allInstanceOf(array $array, $class)

Checks whether every value in an array is an instance of a class.

Details

static mixed findByKeyOrNull(array $array, mixed $key)

Find an item in an associative array by a key value, or return null if not found.

Parameters

array $array
  • The array to search
mixed $key
  • The key to search for.

Return Value

mixed The value of the item in the array with the given key, or null if not found.

static mixed findByKeyOrDefault(array $array, mixed $key, mixed $default)

Find an item in an associative array by a key value, or return default if not found.

Parameters

array $array
  • The array to search
mixed $key
  • The key to search for.
mixed $default
  • The value to return if key is not found.

Return Value

mixed The value of the item in the array with the given key, or the given default if not found.

static arrayMergeIgnoreCase()

Merge a number of arrays using a case insensitive comparison for the array keys.

Exceptions

InvalidArgumentException If less than two arrays are passed to the function, or one of the arguments is not an array.

static bool isAssociative(array $arr)

Checks whether an array's keys are associative. An array's keys are associate if they are not values 0 to count(array) - 1.

Parameters

array $arr array The array whos keys will be checked.

Return Value

bool True if the array's keys are associative. Also true in the case of an empty array.

static bool all(array $array, callable $predicate)

Checks whether every value in an array passes the provided predicate.

Parameters

array $array array The array to test.
callable $predicate callable A predicate which should take one argument and return a boolean.

Return Value

bool Whether every value in the array passes the predicate.

static bool allInstanceOf(array $array, $class)

Checks whether every value in an array is an instance of a class.

Parameters

array $array array The array to test.
$class The fully qualified class name to check every array value with.

Return Value

bool Whether every value in the array is an instance of $class.