ArrayHelper
class ArrayHelper (View source)
The ArrayHelper class
Methods
Utility function to map an array to a stdClass object.
Utility function to convert all types to an array.
Extracts a column from an array of arrays or objects
Utility function to return a value from a named array or a specified default
Set a value into array or object.
Takes an associative array of arrays and inverts the array keys to values using the array values as keys.
Method to determine if an array is an associative array.
Re-group an array to create a reverse lookup of an array of scalars, arrays or objects.
Pivot Array, separate by key.
Utility function to sort an array of objects on a given field
Multidimensional array safe unique test
An improved array_search that allows for partial matching of strings values in associative arrays.
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):
Get data from array or object by path.
setByPath
Recursive dump variables and limit by level.
Method to recursively convert data to one dimension array.
Query a two-dimensional array values to get second level array.
Check an array match our query.
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.
at line line 57
static array
toArray(mixed $data, bool $recursive = false)
Utility function to convert all types to an array.
at line line 97
static array
getColumn(array $array, string $index)
Extracts a column from an array of arrays or objects
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
at line line 207
static mixed
setValue(mixed $array, string $key, mixed $value)
Set a value into 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' );
at line line 281
static boolean
isAssociative(array $array)
Method to determine if an 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.
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 )
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
at line line 505
static array
arrayUnique(array $array)
Multidimensional array safe unique test
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.
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.
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'].
at line line 650
static boolean
setByPath(mixed $data, string $path, mixed $value, string $separator = '.', string $storeType = 'array')
setByPath
at line line 729
static string
dump(mixed $data, int $level = 5)
Recursive dump variables and limit by level.
at line line 830
static array
flatten(array|object $array, string $separator = '.', string $prefix = '')
Method to recursively convert data to one dimension 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.
at line line 923
static bool
match(array $array, array $queries, boolean $strict = false)
Check an array match our query.
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.