class ArrayHelper (View source)

The ArrayHelper class

Methods

static object
toObject(array $array, string $class = 'stdClass')

Utility function to map an array to a stdClass object.

static array
toArray(mixed $data, bool $recursive = false)

Utility function to convert all types to an array.

static array
getColumn(array $array, string $index)

Extracts a column from an array of arrays or objects

static mixed
getValue(array $source, string $name, mixed $default = null, string $type = '')

Utility function to return a value from a named array or a specified default

static mixed
setValue(mixed $array, string $key, mixed $value)

Set a value into array or object.

static array
invert(array $array)

Takes an associative array of arrays and inverts the array keys to values using the array values as keys.

static boolean
isAssociative(array $array)

Method to determine if an array is an associative array.

static array
group(array $source, string $key = null)

Re-group an array to create a reverse lookup of an array of scalars, arrays or objects.

static array
pivot(array $array)

Pivot Array, separate by key.

static array
sortObjects(array $a, mixed $k, mixed $direction = 1, mixed $caseSensitive = true, mixed $locale = false)

Utility function to sort an array of objects on a given field

static array
arrayUnique(array $array)

Multidimensional array safe unique test

static mixed
arraySearch(string $needle, array $haystack, boolean $caseSensitive = true)

An improved array_search that allows for partial matching of strings values in associative arrays.

static array
merge(array $array1, array $array2, boolean $recursive = true)

arraymergerecursive does indeed merge arrays, but it converts values with duplicate keys to arrays rather than overwriting the value in the first array with the duplicate value in the second array, as arraymerge does. I.e., with arraymerge_recursive, this happens (documented behavior):

static mixed
getByPath(mixed $data, mixed $path, string $separator = '.')

Get data from array or object by path.

static boolean
setByPath(mixed $data, string $path, mixed $value, string $separator = '.', string $storeType = 'array')

setByPath

static string
dump(mixed $data, int $level = 5)

Recursive dump variables and limit by level.

static array
flatten(array|object $array, string $separator = '.', string $prefix = '')

Method to recursively convert data to one dimension array.

static array
query(array $array, array|callable $queries = array(), boolean $strict = false, boolean $keepKey = false)

Query a two-dimensional array values to get second level array.

static bool
match(array $array, array $queries, boolean $strict = false)

Check an array match our query.

static mixed
mapKey(mixed $origin, mixed $map = array())

Convert an Array or Object keys to new name by an array index.

Details

at line line 30
static object toObject(array $array, string $class = 'stdClass')

Utility function to map an array to a stdClass object.

Parameters

array $array The array to map.
string $class Name of the class to create

Return Value

object The object mapped from the given array

at line line 57
static array toArray(mixed $data, bool $recursive = false)

Utility function to convert all types to an array.

Parameters

mixed $data The data to convert.
bool $recursive Recursive if data is nested.

Return Value

array The converted array.

at line line 97
static array getColumn(array $array, string $index)

Extracts a column from an array of arrays or objects

Parameters

array $array The source array
string $index The index of the column or name of object property

Return Value

array Column of values from the source array

at line line 128
static mixed getValue(array $source, string $name, mixed $default = null, string $type = '')

Utility function to return a value from a named array or a specified default

Parameters

array $source A named array or object.
string $name The key to search for
mixed $default The default value to give if no key found
string $type Return type for the variable (INT, FLOAT, STRING, WORD, BOOLEAN, ARRAY)

Return Value

mixed The value from the source array

at line line 207
static mixed setValue(mixed $array, string $key, mixed $value)

Set a value into array or object.

Parameters

mixed $array &$array An array to set value.
string $key Array key to store this value.
mixed $value Value which to set into array or object.

Return Value

mixed Result array or object.

at line line 248
static array invert(array $array)

Takes an associative array of arrays and inverts the array keys to values using the array values as keys.

Example: $input = array( 'New' => array('1000', '1500', '1750'), 'Used' => array('3000', '4000', '5000', '6000') ); $output = ArrayHelper::invert($input);

Output would be equal to: $output = array( '1000' => 'New', '1500' => 'New', '1750' => 'New', '3000' => 'Used', '4000' => 'Used', '5000' => 'Used', '6000' => 'Used' );

Parameters

array $array The source array.

Return Value

array The inverted array.

at line line 281
static boolean isAssociative(array $array)

Method to determine if an array is an associative array.

Parameters

array $array An array to test.

Return Value

boolean True if the array is an associative array.

at line line 307
static array group(array $source, string $key = null)

Re-group an array to create a reverse lookup of an array of scalars, arrays or objects.

Parameters

array $source The source array.
string $key Where the elements of the source array are objects or arrays, the key to pivot on.

Return Value

array An array of arrays grouped either on the value of the keys, or an individual key of an object or array.

at line line 401
static array pivot(array $array)

Pivot Array, separate by key.

From: [value] => Array ( [0] => aaa [1] => bbb ) [text] => Array ( [0] => aaa [1] => bbb ) To: [0] => Array ( [value] => aaa [text] => aaa ) [1] => Array ( [value] => bbb [text] => bbb )

Parameters

array $array An array with two level.

Return Value

array An pivoted array.

at line line 431
static array sortObjects(array $a, mixed $k, mixed $direction = 1, mixed $caseSensitive = true, mixed $locale = false)

Utility function to sort an array of objects on a given field

Parameters

array $a An array of objects
mixed $k The key (string) or a array of key to sort on
mixed $direction Direction (integer) or an array of direction to sort in [1 = Ascending] [-1 = Descending]
mixed $caseSensitive Boolean or array of booleans to let sort occur case sensitive or insensitive
mixed $locale Boolean or array of booleans to let sort occur using the locale language or not

Return Value

array The sorted array of objects

at line line 505
static array arrayUnique(array $array)

Multidimensional array safe unique test

Parameters

array $array The array to make unique.

Return Value

array

See also

http://php.net/manual/en/function.array-unique.php

at line line 526
static mixed arraySearch(string $needle, array $haystack, boolean $caseSensitive = true)

An improved array_search that allows for partial matching of strings values in associative arrays.

Parameters

string $needle The text to search for within the array.
array $haystack Associative array to search in to find $needle.
boolean $caseSensitive True to search case sensitive, false otherwise.

Return Value

mixed Returns the matching array $key if found, otherwise false.

at line line 571
static array merge(array $array1, array $array2, boolean $recursive = true)

arraymergerecursive does indeed merge arrays, but it converts values with duplicate keys to arrays rather than overwriting the value in the first array with the duplicate value in the second array, as arraymerge does. I.e., with arraymerge_recursive, this happens (documented behavior):

arraymergerecursive(array('key' => 'org value'), array('key' => 'new value')); => array('key' => array('org value', 'new value'));

arraymergerecursivedistinct does not change the datatypes of the values in the arrays. Matching keys' values in the second array overwrite those in the first array, as is the case with arraymerge, i.e.:

arraymergerecursive_distinct(array('key' => 'org value'), array('key' => 'new value')); => array('key' => array('new value'));

Parameters are passed by reference, though only for performance reasons. They're not altered by this function.

Parameters

array $array1 &$array1 Array to be merge.
array $array2 &$array2 Array to be merge.
boolean $recursive Recursive merge, default is true.

Return Value

array Merged array.

at line line 603
static mixed getByPath(mixed $data, mixed $path, string $separator = '.')

Get data from array or object by path.

Example: ArrayHelper::getByPath($array, 'foo.bar.yoo') equals to $array['foo']['bar']['yoo'].

Parameters

mixed $data An array or object to get value.
mixed $path The key path.
string $separator Separator of paths.

Return Value

mixed Found value, null if not exists.

at line line 650
static boolean setByPath(mixed $data, string $path, mixed $value, string $separator = '.', string $storeType = 'array')

setByPath

Parameters

mixed $data &$data
string $path
mixed $value
string $separator
string $storeType

Return Value

boolean

at line line 729
static string dump(mixed $data, int $level = 5)

Recursive dump variables and limit by level.

Parameters

mixed $data The variable you want to dump.
int $level The level number to limit recursive loop.

Return Value

string Dumped data.

at line line 830
static array flatten(array|object $array, string $separator = '.', string $prefix = '')

Method to recursively convert data to one dimension array.

Parameters

array|object $array The array or object to convert.
string $separator The key separator.
string $prefix Last level key prefix.

Return Value

array

at line line 877
static array query(array $array, array|callable $queries = array(), boolean $strict = false, boolean $keepKey = false)

Query a two-dimensional array values to get second level array.

Parameters

array $array An array to query.
array|callable $queries Query strings or callback, may contain Comparison Operators: '>', '>=', '<', '<='. Example: array( 'id' => 6, // Get all elements where id=6 'published >' => 0 // Get all elements where published>0 );
boolean $strict Use strict to compare equals.
boolean $keepKey Keep origin array keys.

Return Value

array An new two-dimensional array queried.

at line line 923
static bool match(array $array, array $queries, boolean $strict = false)

Check an array match our query.

Parameters

array $array An array to query.
array $queries Query strings or callback, may contain Comparison Operators: '>', '>=', '<', '<='.
boolean $strict Use strict to compare equals.

Return Value

bool

at line line 979
static mixed mapKey(mixed $origin, mixed $map = array())

Convert an Array or Object keys to new name by an array index.

Parameters

mixed $origin Array or Object to convert.
mixed $map Array or Object index for convert.

Return Value

mixed Mapped array or object.