CRM_Utils_Array
class CRM_Utils_Array
Provides a collection of static methods for array manipulation.
Methods
Returns $list[$key] if such element exists, or a default value otherwise.
Recursively searches an array for a key, returning the first value found.
Wraps and slightly changes the behavior of PHP's array_search().
Builds an XML fragment representing an array.
Sanitizes a string for serialization in CRM_Utils_Array::xml().
Converts a nested array to a flat array.
Converts an array with path-like keys into a tree of arrays.
Merges two arrays.
Determines whether an array contains any sub-arrays.
Is array A a subset of array B.
Searches an array recursively in an optionally case-insensitive manner.
Convert associative array names to values and vice-versa.
Checks whether an array is empty.
Sorts an associative array of arrays by an attribute using strnatcmp().
Recursively removes duplicate values from a multi-dimensional array.
Sorts an array and maintains index association (with localization).
Unsets an arbitrary list of array elements from an associative array.
Builds an array-tree which indexes the records in an array.
Iterates over a list of records and returns the value of some property.
Iterates over a list of objects and executes some method on each.
Trims delimiters from a string and then splits it using explode().
Joins array elements with a string, adding surrounding delimiters.
Modifies a key in an array while preserving the key order.
Searches array keys by regex, returning the value of the first match.
Generates the Cartesian product of zero or more vectors.
Get the first element of an array.
Extract any $keys from $array and copy to a new array.
Transform an associative array of key=>value pairs into a non-associative array of arrays.
Diff multidimensional arrays (array_diff does not support multidimensional array)
Given a 2-dimensional matrix, create a new matrix with a restricted list of columns.
Rewrite the keys in an array.
Copy all properties of $other into $array (recursively).
Get a single value from an array-tree.
Check if a key isset which may be several layers deep.
Set a single value in an array tree.
Convert a simple dictionary into separate key+value records.
Convert array where key(s) holds the actual value and value(s) as 1 into array of actual values Ex: array('foobar' => 1, 4 => 1) formatted into array('foobar', 4)
Convert the data format coming in from checkboxes to an array of values.
Ensure that array is encoded in utf8 format.
Build tree of elements.
Find search string in tree.
Details
at line 53
static mixed
value(string $key, array $list, mixed $default = NULL)
Returns $list[$key] if such element exists, or a default value otherwise.
If $list is not actually an array at all, then the default value is returned.
at line 74
static mixed
retrieveValueRecursive(array $params, string $key)
Recursively searches an array for a key, returning the first value found.
If $params[$key] does not exist and $params contains arrays, descend into each array in a depth-first manner, in array iteration order.
at line 109
static int|string|null
key(mixed $value, array $list)
Wraps and slightly changes the behavior of PHP's array_search().
This function reproduces the behavior of array_search() from PHP prior to version 4.2.0, which was to return NULL on failure. This function also checks that $list is an array before attempting to search it.
at line 138
static string
xml(array $list, int $depth = 1, string $seperator = "\n")
Builds an XML fragment representing an array.
Depending on the nature of the keys of the array (and its sub-arrays, if any) the XML fragment may not be valid.
at line 169
static string
escapeXML(string $value)
Sanitizes a string for serialization in CRM_Utils_Array::xml().
Replaces '&', '<', and '>' with their XML escape sequences. Replaces '^A' with a comma.
at line 234
static
flatten(array $list, array $flat, string $prefix = '', string $seperator = ".")
Converts a nested array to a flat array.
The nested structure is preserved in the string values of the keys of the flat array.
Example nested array: Array ( [foo] => Array ( [0] => bar [1] => baz [2] => 42 )
[asdf] => Array
(
[merp] => bleep
[quack] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
[quux] => 999
)
Corresponding flattened array: Array ( [foo.0] => bar [foo.1] => baz [foo.2] => 42 [asdf.merp] => bleep [asdf.quack.0] => 1 [asdf.quack.1] => 2 [asdf.quack.2] => 3 [quux] => 999 )
at line 259
array
unflatten(string $delim, array $arr)
Converts an array with path-like keys into a tree of arrays.
This function is the inverse of CRM_Utils_Array::flatten().
at line 294
static array
crmArrayMerge(array $a1, array $a2)
Merges two arrays.
If $a1[foo] and $a2[foo] both exist and are both arrays, the merge process recurses into those sub-arrays. If $a1[foo] and $a2[foo] both exist but they are not both arrays, the value from $a1 overrides the value from $a2 and the value from $a2 is discarded.
at line 335
static bool
isHierarchical(array $list)
Determines whether an array contains any sub-arrays.
at line 353
static bool
isSubset(array $subset, array $superset)
Is array A a subset of array B.
at line 375
static bool
crmInArray(string $value, array $params, bool $caseInsensitive = TRUE)
Searches an array recursively in an optionally case-insensitive manner.
at line 402
static bool
lookupValue(array $defaults, string $property, $lookup, $reverse)
Convert associative array names to values and vice-versa.
This function is used by by import functions and some webforms.
at line 450
static bool
crmIsEmptyArray(array $array = [])
Checks whether an array is empty.
An array is empty if its values consist only of NULL and empty sub-arrays. Containing a non-NULL value or non-empty array makes an array non-empty.
If something other than an array is passed, it is considered to be empty.
If nothing is passed at all, the default value provided is empty.
at line 478
static array
crmArraySortByField(array $array, string|array $field)
Sorts an associative array of arrays by an attribute using strnatcmp().
at line 501
static array
crmArrayUnique(array $array)
Recursively removes duplicate values from a multi-dimensional array.
at line 525
static array
asort(array $array = [])
Sorts an array and maintains index association (with localization).
Uses Collate from the PECL "intl" package, if available, for UTF-8 sorting (e.g. list of countries). Otherwise calls PHP's asort().
On Debian/Ubuntu: apt-get install php5-intl
at line 550
static
remove(array $items)
Unsets an arbitrary list of array elements from an associative array.
at line 574
static array
index(string[] $keys, object|array $records)
Builds an array-tree which indexes the records in an array.
at line 613
static array
collect(string $prop, array|object $records)
Iterates over a list of records and returns the value of some property.
at line 647
static array
collectMethod(string $method, array|Traversable $objects, array $args = [])
Iterates over a list of objects and executes some method on each.
Comparison: - This is like array_map(), except it executes the objects' method instead of a free-form callable. - This is like Array::collect(), except it uses a method instead of a property.
at line 675
static array|null
explodePadded(array|null|string $values, string $delim = CRM_Core_DAO::VALUE_SEPARATOR)
Trims delimiters from a string and then splits it using explode().
This method works mostly like PHP's built-in explode(), except that surrounding delimiters are trimmed before explode() is called.
Also, if an array or NULL is passed as the $values parameter, the value is returned unmodified rather than being passed to explode().
at line 707
static string|NULL
implodePadded(mixed $values, string $delim = CRM_Core_DAO::VALUE_SEPARATOR)
Joins array elements with a string, adding surrounding delimiters.
This method works mostly like PHP's built-in implode(), but the generated string is surrounded by delimiter characters. Also, if NULL is passed as the $values parameter, NULL is returned.
at line 740
static array
crmReplaceKey(array $elementArray, string $oldKey, string $newKey)
Modifies a key in an array while preserving the key order.
By default when an element is added to an array, it is added to the end. This method allows for changing an existing key while preserving its position in the array.
The array is both modified in-place and returned.
at line 771
static mixed
valueByRegexKey(string $regexKey, array $list, mixed $default = NULL)
Searches array keys by regex, returning the value of the first match.
Given a regular expression and an array, this method searches the keys of the array using the regular expression. The first match is then used to index into the array, and the associated value is retrieved and returned. If no matches are found, or if something other than an array is passed, then a default value is returned. Unless otherwise specified, the default value is NULL.
at line 805
static array
product(array $dimensions, array $template = [])
Generates the Cartesian product of zero or more vectors.
at line 834
static mixed|NULL
first(array $array)
Get the first element of an array.
at line 852
static array
subset(array $array, array $keys)
Extract any $keys from $array and copy to a new array.
Note: If a $key does not appear in $array, then it will not appear in the result.
at line 875
static array
makeNonAssociative(array $associative, string $keyName = 'key', string $valueName = 'value')
Transform an associative array of key=>value pairs into a non-associative array of arrays.
This is necessary to preserve sort order when sending an array through json_encode.
at line 891
static array
multiArrayDiff(array $array1, array $array2)
Diff multidimensional arrays (array_diff does not support multidimensional array)
at line 923
static array
filterColumns(array $matrix, array $columns)
Given a 2-dimensional matrix, create a new matrix with a restricted list of columns.
at line 943
static array
rekey(array $array, string|callable $indexBy)
Rewrite the keys in an array.
at line 958
static
extend(array|ArrayAccess $array, array $other)
Copy all properties of $other into $array (recursively).
at line 980
static mixed
pathGet(array $values, array $path, mixed $default = NULL)
Get a single value from an array-tree.
at line 1000
static bool
pathIsset(array $values, array $path)
Check if a key isset which may be several layers deep.
This is a helper for when the calling function does not know how many layers deep the path array is so cannot easily check.
at line 1020
static
pathSet(array $values, array $pathParts, $value)
Set a single value in an array tree.
at line 1044
static array
toKeyValueRows(array $array, string $keyField = 'key', string $valueField = 'value')
deprecated
deprecated
Convert a simple dictionary into separate key+value records.
at line 1057
static
formatArrayKeys(array $array)
deprecated
deprecated
Convert array where key(s) holds the actual value and value(s) as 1 into array of actual values Ex: array('foobar' => 1, 4 => 1) formatted into array('foobar', 4)
at line 1088
static array
convertCheckboxFormatToArray(array $input)
Convert the data format coming in from checkboxes to an array of values.
The input format from check boxes looks like array('value1' => 1, 'value2' => 1). This function converts those values to array(''value1', 'value2).
The function will only alter the array if all values are equal to 1.
at line 1106
static array
encode_items(array $array)
Ensure that array is encoded in utf8 format.
at line 1129
static array
buildTree(array $elements, int|null $parentId = NULL)
Build tree of elements.
at line 1154
static array|null
findInTree(string $search, array $tree, string $field = 'id')
Find search string in tree.