Documentation

CRM_Utils_Array
in package

Provides a collection of static methods for array manipulation.

Tags
copyright

CiviCRM LLC https://civicrm.org/licensing

Table of Contents

Methods

asColumns()  : array<string|int, mixed>
Rotate a matrix, converting from row-oriented array to a column-oriented array.
asort()  : array<string|int, mixed>
Sorts an array and maintains index association (with localization).
buildTree()  : array<string|int, mixed>
Build tree of elements.
cast()  : array<string|int, mixed>
Cast a value to an array.
collect()  : array<string|int, mixed>
Iterates over a list of records and returns the value of some property.
collectMethod()  : array<string|int, mixed>
Iterates over a list of objects and executes some method on each.
convertCheckboxFormatToArray()  : array<string|int, mixed>
Convert the data format coming in from checkboxes to an array of values.
crmArrayMerge()  : array<string|int, mixed>
Merges two arrays.
crmArraySortByField()  : array<string|int, mixed>
Sorts an associative array of arrays by an attribute using strnatcmp().
crmArrayUnique()  : array<string|int, mixed>
Recursively removes duplicate values from a multi-dimensional array.
crmInArray()  : bool
Searches an array recursively in an optionally case-insensitive manner.
crmIsEmptyArray()  : bool
Checks whether an array is empty.
crmReplaceKey()  : array<string|int, mixed>
Modifies a key in an array while preserving the key order.
encode_items()  : array<string|int, mixed>
Ensure that array is encoded in utf8 format.
escapeXML()  : string
Sanitizes a string for serialization in CRM_Utils_Array::xml().
explodePadded()  : array<string|int, mixed>|null
Trims delimiters from a string and then splits it using explode().
extend()  : mixed
Copy all properties of $other into $array (recursively).
filterByPrefix()  : array<string|int, mixed>
Removes all items from an array whose keys have a given prefix, and returns them unprefixed.
filterColumns()  : array<string|int, mixed>
Given a 2-dimensional matrix, create a new matrix with a restricted list of columns.
findAll()  : array<string|int, mixed>
Recursively searches through a given array for all matches
findInTree()  : array<string|int, mixed>|null
Find search string in tree.
first()  : mixed|null
Get the first element of an array.
flatten()  : mixed
Converts a nested array to a flat array.
formatArrayKeys()  : mixed
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)
formatForSelect2()  : array<string|int, mixed>
Changes array keys to meet the expectations of select2.js
implodePadded()  : string|null
Joins array elements with a string, adding surrounding delimiters.
index()  : array<string|int, mixed>
Builds an array-tree which indexes the records in an array.
isHierarchical()  : bool
Determines whether an array contains any sub-arrays.
isSubset()  : bool
Is array A a subset of array B.
key()  : int|string|null
Wraps and slightly changes the behavior of PHP's array_search().
lookupValue()  : bool
Convert associative array names to values and vice-versa.
makeNonAssociative()  : array<string|int, mixed>
Transform an associative array of key=>value pairs into a non-associative array of arrays.
multiArrayDiff()  : array<string|int, mixed>
Diff multidimensional arrays (array_diff does not support multidimensional array)
pathGet()  : mixed
Get a single value from an array-tree.
pathIsset()  : bool
Check if a key isset which may be several layers deep.
pathMove()  : int
Move an item in an array-tree (if it exists).
pathSet()  : mixed
Set a single value in an array tree.
pathSync()  : mixed
Attempt to synchronize or fill aliased items.
pathUnset()  : bool
Remove a key from an array.
prefixKeys()  : array<string|int, mixed>
Prepend string prefix to every key in an array
product()  : array<string|int, mixed>
Generates the Cartesian product of zero or more vectors.
rekey()  : array<string|int, mixed>
Rewrite the keys in an array.
remove()  : mixed
Unsets an arbitrary list of array elements from an associative array.
retrieveValueRecursive()  : mixed
Recursively searches an array for a key, returning the first value found.
single()  : mixed
Take one well-defined item out of a single-item list.
subset()  : array<string|int, mixed>
Extract any $keys from $array and copy to a new array.
toKeyValueRows()  : array<string|int, mixed>
Convert a simple dictionary into separate key+value records.
unflatten()  : array<string|int, mixed>
Converts an array with path-like keys into a tree of arrays.
value()  : mixed
Returns $list[$key] if such element exists, or $default otherwise.
valueByRegexKey()  : mixed
Searches array keys by regex, returning the value of the first match.
xml()  : string
Builds an XML fragment representing an array.

Methods

asColumns()

Rotate a matrix, converting from row-oriented array to a column-oriented array.

public static asColumns(iterable<string|int, mixed> $rows[, bool $unique = FALSE ]) : array<string|int, mixed>
Parameters
$rows : iterable<string|int, mixed>

Ex: [['a'=>10,'b'=>'11'], ['a'=>20,'b'=>21]] Formula: [scalar $rowId => [scalar $colId => mixed $value]]

$unique : bool = FALSE

Only return unique values.

Return values
array<string|int, mixed>

Ex: ['a'=>[10,20], 'b'=>[11,21]] Formula: [scalar $colId => [scalar $rowId => mixed $value]] Note: In unique mode, the $rowId is not meaningful.

asort()

Sorts an array and maintains index association (with localization).

public static asort([array<string|int, mixed> $array = [] ]) : array<string|int, mixed>

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

Parameters
$array : array<string|int, mixed> = []

(optional) Array to be sorted.

Return values
array<string|int, mixed>

Sorted array.

buildTree()

Build tree of elements.

public static buildTree(array<string|int, mixed> $elements[, int|null $parentId = NULL ]) : array<string|int, mixed>
Parameters
$elements : array<string|int, mixed>
$parentId : int|null = NULL
Return values
array<string|int, mixed>

cast()

Cast a value to an array.

public static cast(mixed $value) : array<string|int, mixed>

This is similar to PHP's (array), but it also converts iterators.

Parameters
$value : mixed
Return values
array<string|int, mixed>

collect()

Iterates over a list of records and returns the value of some property.

public static collect(string $prop, array<string|int, mixed>|object $records) : array<string|int, mixed>
Parameters
$prop : string

Property to retrieve.

$records : array<string|int, mixed>|object

A list of records.

Return values
array<string|int, mixed>

Keys are the original keys of $records; values are the $prop values.

collectMethod()

Iterates over a list of objects and executes some method on each.

public static collectMethod(string $method, array<string|int, mixed>|Traversable $objects[, array<string|int, mixed> $args = [] ]) : array<string|int, mixed>

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.
Parameters
$method : string

The method to execute.

$objects : array<string|int, mixed>|Traversable

A list of objects.

$args : array<string|int, mixed> = []

An optional list of arguments to pass to the method.

Return values
array<string|int, mixed>

Keys are the original keys of $objects; values are the method results.

convertCheckboxFormatToArray()

Convert the data format coming in from checkboxes to an array of values.

public static convertCheckboxFormatToArray(array<string|int, mixed> $input) : array<string|int, mixed>

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.

Parameters
$input : array<string|int, mixed>
Return values
array<string|int, mixed>

crmArrayMerge()

Merges two arrays.

public static crmArrayMerge(array<string|int, mixed> $a1, array<string|int, mixed> $a2) : array<string|int, mixed>

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.

Parameters
$a1 : array<string|int, mixed>

First array to be merged.

$a2 : array<string|int, mixed>

Second array to be merged.

Return values
array<string|int, mixed>

The merged array.

crmArraySortByField()

Sorts an associative array of arrays by an attribute using strnatcmp().

public static crmArraySortByField(array<string|int, mixed> $array, string|array<string|int, mixed> $field) : array<string|int, mixed>
Parameters
$array : array<string|int, mixed>

Array to be sorted.

$field : string|array<string|int, mixed>

Name of the attribute used for sorting.

Return values
array<string|int, mixed>

Sorted array

crmArrayUnique()

Recursively removes duplicate values from a multi-dimensional array.

public static crmArrayUnique(array<string|int, mixed> $array) : array<string|int, mixed>
Parameters
$array : array<string|int, mixed>

The input array possibly containing duplicate values.

Return values
array<string|int, mixed>

The input array with duplicate values removed.

crmInArray()

Searches an array recursively in an optionally case-insensitive manner.

public static crmInArray(string $value, array<string|int, mixed> $params[, bool $caseInsensitive = TRUE ]) : bool
Parameters
$value : string

Value to search for.

$params : array<string|int, mixed>

Array to search within.

$caseInsensitive : bool = TRUE

(optional) Whether to search in a case-insensitive manner.

Return values
bool

True if $value was found, false otherwise.

crmIsEmptyArray()

Checks whether an array is empty.

public static crmIsEmptyArray([array<string|int, mixed> $array = [] ]) : bool

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.

Parameters
$array : array<string|int, mixed> = []

(optional) Array to be checked for emptiness.

Return values
bool

True if the array is empty.

crmReplaceKey()

Modifies a key in an array while preserving the key order.

public static crmReplaceKey(array<string|int, mixed> &$elementArray, string $oldKey, string $newKey) : array<string|int, mixed>

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.

Parameters
$elementArray : array<string|int, mixed>

Array to manipulate.

$oldKey : string

Old key to be replaced.

$newKey : string

Replacement key string.

Tags
throws
Exception

Throws a generic Exception if $oldKey is not found in $elementArray.

Return values
array<string|int, mixed>

The manipulated array.

encode_items()

Ensure that array is encoded in utf8 format.

public static encode_items(array<string|int, mixed> $array) : array<string|int, mixed>
Parameters
$array : array<string|int, mixed>
Return values
array<string|int, mixed>

$array utf8-encoded.

escapeXML()

Sanitizes a string for serialization in CRM_Utils_Array::xml().

public static escapeXML(string $value) : string

Replaces '&', '<', and '>' with their XML escape sequences. Replaces '^A' with a comma.

Parameters
$value : string

String to be sanitized.

Return values
string

Sanitized version of $value.

explodePadded()

Trims delimiters from a string and then splits it using explode().

public static explodePadded(array<string|int, mixed>|null|string $values[, string $delim = CRM_Core_DAO::VALUE_SEPARATOR ]) : array<string|int, mixed>|null

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().

Parameters
$values : array<string|int, mixed>|null|string

The input string (or an array, or NULL).

$delim : string = CRM_Core_DAO::VALUE_SEPARATOR

(optional) The boundary string.

Return values
array<string|int, mixed>|null

An array of strings produced by explode(), or the unmodified input array, or NULL.

extend()

Copy all properties of $other into $array (recursively).

public static extend(array<string|int, mixed>|ArrayAccess &$array, array<string|int, mixed> $other) : mixed
Parameters
$array : array<string|int, mixed>|ArrayAccess
$other : array<string|int, mixed>

filterByPrefix()

Removes all items from an array whose keys have a given prefix, and returns them unprefixed.

public static filterByPrefix(array<string|int, mixed> &$collection, string $prefix) : array<string|int, mixed>
Parameters
$collection : array<string|int, mixed>
$prefix : string
Return values
array<string|int, mixed>

filterColumns()

Given a 2-dimensional matrix, create a new matrix with a restricted list of columns.

public static filterColumns(array<string|int, mixed> $matrix, array<string|int, mixed> $columns) : array<string|int, mixed>
Parameters
$matrix : array<string|int, mixed>

All matrix data, as a list of rows.

$columns : array<string|int, mixed>

List of column names.

Return values
array<string|int, mixed>

findAll()

Recursively searches through a given array for all matches

public static findAll(array<string|int, mixed>|null $collection, array<string|int, mixed>|callable|string $predicate) : array<string|int, mixed>
Parameters
$collection : array<string|int, mixed>|null
$predicate : array<string|int, mixed>|callable|string
Return values
array<string|int, mixed>

findInTree()

Find search string in tree.

public static findInTree(string $search, array<string|int, mixed> $tree[, string $field = 'id' ]) : array<string|int, mixed>|null
Parameters
$search : string
$tree : array<string|int, mixed>
$field : string = 'id'
Return values
array<string|int, mixed>|null

first()

Get the first element of an array.

public static first(array<string|int, mixed> $array) : mixed|null
Parameters
$array : array<string|int, mixed>
Return values
mixed|null

flatten()

Converts a nested array to a flat array.

public static flatten(array<string|int, mixed> &$list, array<string|int, mixed> &$flat[, string $prefix = '' ][, string $separator = "." ]) : mixed

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 )

Parameters
$list : array<string|int, mixed>

Array to be flattened.

$flat : array<string|int, mixed>

Destination array.

$prefix : string = ''

(optional) String to prepend to keys.

$separator : string = "."

(optional) String that separates the concatenated keys.

formatArrayKeys()

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)

public static formatArrayKeys(array<string|int, mixed> &$array) : mixed

use convertCheckboxInputToArray instead (after testing) https://github.com/civicrm/civicrm-core/pull/8169

Parameters
$array : array<string|int, mixed>

formatForSelect2()

Changes array keys to meet the expectations of select2.js

public static formatForSelect2(array<string|int, mixed> $options[, string $label = 'label' ][, string $id = 'id' ]) : array<string|int, mixed>
Parameters
$options : array<string|int, mixed>
$label : string = 'label'
$id : string = 'id'
Return values
array<string|int, mixed>

implodePadded()

Joins array elements with a string, adding surrounding delimiters.

public static implodePadded(mixed $values[, string $delim = CRM_Core_DAO::VALUE_SEPARATOR ]) : string|null

This method works mostly like PHP's built-in implode(), but the generated string is surrounded by delimiter characters. Also, if NULL or '' is passed as the $values parameter, it is returned unchanged.

Parameters
$values : mixed

Array to be imploded. If a non-array is passed, it will be cast to an array.

$delim : string = CRM_Core_DAO::VALUE_SEPARATOR

Delimiter to be used for implode() and which will surround the output string.

Return values
string|null

The generated string, or NULL if NULL was passed as $values parameter.

index()

Builds an array-tree which indexes the records in an array.

public static index(array<string|int, string> $keys, object|array<string|int, mixed> $records) : array<string|int, mixed>
Parameters
$keys : array<string|int, string>

Properties by which to index.

$records : object|array<string|int, mixed>
Return values
array<string|int, mixed>

Multi-dimensional array, with one layer for each key.

isHierarchical()

Determines whether an array contains any sub-arrays.

public static isHierarchical(array<string|int, mixed> &$list) : bool
Parameters
$list : array<string|int, mixed>

The array to inspect.

Return values
bool

True if $list contains at least one sub-array, false otherwise.

isSubset()

Is array A a subset of array B.

public static isSubset(array<string|int, mixed> $subset, array<string|int, mixed> $superset) : bool
Parameters
$subset : array<string|int, mixed>
$superset : array<string|int, mixed>
Return values
bool

TRUE if $subset is a subset of $superset

key()

Wraps and slightly changes the behavior of PHP's array_search().

public static key(mixed $value, array<string|int, mixed> $list) : int|string|null

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.

Parameters
$value : mixed

The value to search for.

$list : array<string|int, mixed>

The array to be searched.

Return values
int|string|null

Returns the key, which could be an int or a string, or NULL on failure.

lookupValue()

Convert associative array names to values and vice-versa.

public static lookupValue(array<string|int, mixed> &$defaults, string $property, mixed $lookup, mixed $reverse) : bool

This function is used by by import functions and some webforms.

Parameters
$defaults : array<string|int, mixed>
$property : string
$lookup : mixed
$reverse : mixed
Return values
bool

makeNonAssociative()

Transform an associative array of key=>value pairs into a non-associative array of arrays.

public static makeNonAssociative(array<string|int, mixed> $associative[, string $keyName = 'key' ][, string $valueName = 'value' ]) : array<string|int, mixed>

This is necessary to preserve sort order when sending an array through json_encode.

Parameters
$associative : array<string|int, mixed>

Ex: ['foo' => 'bar'].

$keyName : string = 'key'

Ex: 'key'.

$valueName : string = 'value'

Ex: 'value'.

Return values
array<string|int, mixed>

Ex: [0 => ['key' => 'foo', 'value' => 'bar']].

multiArrayDiff()

Diff multidimensional arrays (array_diff does not support multidimensional array)

public static multiArrayDiff(array<string|int, mixed> $array1, array<string|int, mixed> $array2) : array<string|int, mixed>
Parameters
$array1 : array<string|int, mixed>
$array2 : array<string|int, mixed>
Return values
array<string|int, mixed>

pathGet()

Get a single value from an array-tree.

public static pathGet(array<string|int, mixed> $values, array<string|int, mixed> $path[, mixed $default = NULL ]) : mixed
Parameters
$values : array<string|int, mixed>

Ex: ['foo' => ['bar' => 123]].

$path : array<string|int, mixed>

Ex: ['foo', 'bar'].

$default : mixed = NULL
Return values
mixed

Ex 123.

pathIsset()

Check if a key isset which may be several layers deep.

public static pathIsset(array<string|int, mixed> $values, array<string|int, mixed> $path) : bool

This is a helper for when the calling function does not know how many layers deep the path array is so cannot easily check.

Parameters
$values : array<string|int, mixed>
$path : array<string|int, mixed>
Return values
bool

pathMove()

Move an item in an array-tree (if it exists).

public static pathMove(array<string|int, mixed> &$values, array<string|int, string> $src, array<string|int, string> $dest[, bool $cleanup = FALSE ]) : int
Parameters
$values : array<string|int, mixed>

Data-tree

$src : array<string|int, string>

Old path for the existing item

$dest : array<string|int, string>

New path

$cleanup : bool = FALSE
Return values
int

Number of items moved (0 or 1).

pathSet()

Set a single value in an array tree.

public static pathSet(array<string|int, mixed> &$values, array<string|int, mixed> $pathParts, mixed $value) : mixed
Parameters
$values : array<string|int, mixed>

Ex: ['foo' => ['bar' => 123]].

$pathParts : array<string|int, mixed>

Ex: ['foo', 'bar'].

$value : mixed

Ex: 456.

pathSync()

Attempt to synchronize or fill aliased items.

public static pathSync(array<string|int, mixed> &$params, array<string|int, string> $canonPath, array<string|int, string> $altPath[, callable|null $filter = NULL ]) : mixed

If $canonPath is set, copy to $altPath; or... If $altPath is set, copy to $canonPath.

Parameters
$params : array<string|int, mixed>

Data-tree

$canonPath : array<string|int, string>

Preferred path

$altPath : array<string|int, string>

Old/alternate/deprecated path.

$filter : callable|null = NULL

Optional function to filter the value as it passes through (canonPath=>altPath or altPath=>canonPath). function(mixed $v, bool $isCanon): mixed

pathUnset()

Remove a key from an array.

public static pathUnset(array<string|int, mixed> &$values, array<string|int, mixed> $path[, bool $cleanup = FALSE ]) : bool

This is a helper for when the calling function does not know how many layers deep the path array is so cannot easily check.

Parameters
$values : array<string|int, mixed>
$path : array<string|int, mixed>
$cleanup : bool = FALSE

If removed item leaves behind an empty array, should you remove the empty array?

Return values
bool

TRUE if anything has been removed. FALSE if no changes were required.

prefixKeys()

Prepend string prefix to every key in an array

public static prefixKeys(array<string|int, mixed> $collection, string $prefix) : array<string|int, mixed>
Parameters
$collection : array<string|int, mixed>
$prefix : string
Return values
array<string|int, mixed>

product()

Generates the Cartesian product of zero or more vectors.

public static product(array<string|int, mixed> $dimensions[, array<string|int, mixed> $template = [] ]) : array<string|int, mixed>
Parameters
$dimensions : array<string|int, mixed>

List of dimensions to multiply. Each key is a dimension name; each value is a vector.

$template : array<string|int, mixed> = []

(optional) A base set of values included in every output.

Return values
array<string|int, mixed>

Each item is a distinct combination of values from $dimensions.

For example, the product of { fg => {red, blue}, bg => {white, black} } would be { {fg => red, bg => white}, {fg => red, bg => black}, {fg => blue, bg => white}, {fg => blue, bg => black} }

rekey()

Rewrite the keys in an array.

public static rekey(array<string|int, mixed> $array, string|callable $indexBy) : array<string|int, mixed>
Parameters
$array : array<string|int, mixed>
$indexBy : string|callable

Either the value to key by, or a function($key, $value) that returns the new key.

Return values
array<string|int, mixed>

remove()

Unsets an arbitrary list of array elements from an associative array.

public static remove(array<string|int, mixed> &$items) : mixed
Parameters
$items : array<string|int, mixed>

The array from which to remove items.

Additional params: When passed a string, unsets $items[$key]. When passed an array of strings, unsets $items[$k] for each string $k in the array.

retrieveValueRecursive()

Recursively searches an array for a key, returning the first value found.

public static retrieveValueRecursive(array<string|int, mixed> $params, string $key) : mixed

If $params[$key] does not exist and $params contains arrays, descend into each array in a depth-first manner, in array iteration order.

Parameters
$params : array<string|int, mixed>

The array to be searched.

$key : string

The key to search for.

Return values
mixed

The value of the key, or null if the key is not found.

single()

Take one well-defined item out of a single-item list.

public static single(iterable<string|int, mixed> $items[, string $recordTypeLabel = 'record' ]) : mixed

Assert that the list genuinely contains exactly one item.

Parameters
$items : iterable<string|int, mixed>
$recordTypeLabel : string = 'record'
Tags
throws
CRM_Core_Exception
Return values
mixed

The first (and only) item from the $items list.

subset()

Extract any $keys from $array and copy to a new array.

public static subset(array<string|int, mixed> $array, array<string|int, mixed> $keys) : array<string|int, mixed>

Note: If a $key does not appear in $array, then it will not appear in the result.

Parameters
$array : array<string|int, mixed>
$keys : array<string|int, mixed>

List of keys to copy.

Return values
array<string|int, mixed>

toKeyValueRows()

Convert a simple dictionary into separate key+value records.

public static toKeyValueRows(array<string|int, mixed> $array[, string $keyField = 'key' ][, string $valueField = 'value' ]) : array<string|int, mixed>
Parameters
$array : array<string|int, mixed>

Ex: array('foo' => 'bar').

$keyField : string = 'key'

Ex: 'key'.

$valueField : string = 'value'

Ex: 'value'.

Return values
array<string|int, mixed>

unflatten()

Converts an array with path-like keys into a tree of arrays.

public unflatten(string $delim, array<string|int, mixed> &$arr) : array<string|int, mixed>

This function is the inverse of CRM_Utils_Array::flatten().

Parameters
$delim : string

A path delimiter

$arr : array<string|int, mixed>

A one-dimensional array indexed by string keys

Return values
array<string|int, mixed>

Array-encoded tree

value()

Returns $list[$key] if such element exists, or $default otherwise.

public static value(string $key, array<string|int, mixed>|ArrayAccess $list[, mixed $default = NULL ]) : mixed

In most cases this can be replaced with $list[$key] ?? $default with the minor difference that when $list[$key] exists and is NULL, this function will always return NULL.

If $list is not an array or ArrayAccess object, $default is returned.

Parameters
$key : string

Key value to look up in the array.

$list : array<string|int, mixed>|ArrayAccess

Array from which to look up a value.

$default : mixed = NULL

(optional) Value to return when $list[$key] does not exist. If $default is not specified, NULL is used.

Return values
mixed

Can return any type, since $list might contain anything.

valueByRegexKey()

Searches array keys by regex, returning the value of the first match.

public static valueByRegexKey(string $regexKey, array<string|int, mixed> $list[, mixed $default = NULL ]) : mixed

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.

Parameters
$regexKey : string

The regular expression to use when searching for matching keys.

$list : array<string|int, mixed>

The array whose keys will be searched.

$default : mixed = NULL

(optional) The default value to return if the regex does not match an array key, or if something other than an array is passed.

Return values
mixed

The value found.

xml()

Builds an XML fragment representing an array.

public static & xml(array<string|int, mixed> &$list[, int $depth = 1 ][, string $separator = " " ]) : string

Depending on the nature of the keys of the array (and its sub-arrays, if any) the XML fragment may not be valid.

Parameters
$list : array<string|int, mixed>

The array to be serialized.

$depth : int = 1

(optional) Indentation depth counter.

$separator : string = " "

(optional) String to be appended after open/close tags.

Return values
string

XML fragment representing $list.


        
On this page

Search results