Class yii\helpers\BaseArrayHelper
Inheritance | yii\helpers\BaseArrayHelper |
---|---|
Subclasses | yii\helpers\ArrayHelper |
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseArrayHelper.php |
BaseArrayHelper provides concrete implementation for yii\helpers\ArrayHelper.
Do not use BaseArrayHelper. Use yii\helpers\ArrayHelper instead.
Public Methods
Method | Description | Defined By |
---|---|---|
getColumn() | Returns the values of a specified column in an array. | yii\helpers\BaseArrayHelper |
getValue() | Retrieves the value of an array element or object property with the given key or property name. | yii\helpers\BaseArrayHelper |
htmlDecode() | Decodes HTML entities into the corresponding characters in an array of strings. | yii\helpers\BaseArrayHelper |
htmlEncode() | Encodes special characters in an array of strings into HTML entities. | yii\helpers\BaseArrayHelper |
index() | Indexes an array according to a specified key. | yii\helpers\BaseArrayHelper |
isAssociative() | Returns a value indicating whether the given array is an associative array. | yii\helpers\BaseArrayHelper |
isIndexed() | Returns a value indicating whether the given array is an indexed array. | yii\helpers\BaseArrayHelper |
keyExists() | Checks if the given array contains the specified key. | yii\helpers\BaseArrayHelper |
map() | Builds a map (key-value pairs) from a multidimensional array or an array of objects. | yii\helpers\BaseArrayHelper |
merge() | Merges two or more arrays into one recursively. | yii\helpers\BaseArrayHelper |
multisort() | Sorts an array of objects or arrays (with the same structure) by one or several keys. | yii\helpers\BaseArrayHelper |
remove() | Removes an item from an array and returns the value. If the key does not exist in the array, the default value will be returned instead. | yii\helpers\BaseArrayHelper |
toArray() | Converts an object or an array of objects into an array. | yii\helpers\BaseArrayHelper |
Method Details
Returns the values of a specified column in an array.
The input array should be multidimensional or an array of objects.
For example,
$array = [
['id' => '123', 'data' => 'abc'],
['id' => '345', 'data' => 'def'],
];
$result = ArrayHelper::getColumn($array, 'id');
// the result is: ['123', '345']
// using anonymous function
$result = ArrayHelper::getColumn($array, function ($element) {
return $element['id'];
});
array getColumn( $array, $name, $keepKeys = true ) | ||
$array | array | |
$name | string|Closure | |
$keepKeys | boolean | Whether to maintain the array keys. If false, the resulting array will be re-indexed with integers. |
return | array | The list of column values |
---|
Retrieves the value of an array element or object property with the given key or property name.
If the key does not exist in the array or object, the default value will be returned instead.
The key may be specified in a dot format to retrieve the value of a sub-array or the property
of an embedded object. In particular, if the key is x.y.z
, then the returned value would
be $array['x']['y']['z']
or $array->x->y->z
(if $array
is an object). If $array['x']
or $array->x
is neither an array nor an object, the default value will be returned.
Note that if the array already has an element x.y.z
, then its value will be returned
instead of going through the sub-arrays. So it is better to be done specifying an array of key names
like ['x', 'y', 'z']
.
Below are some usage examples,
// working with array
$username = \yii\helpers\ArrayHelper::getValue($_POST, 'username');
// working with object
$username = \yii\helpers\ArrayHelper::getValue($user, 'username');
// working with anonymous function
$fullName = \yii\helpers\ArrayHelper::getValue($user, function ($user, $defaultValue) {
return $user->firstName . ' ' . $user->lastName;
});
// using dot format to retrieve the property of embedded object
$street = \yii\helpers\ArrayHelper::getValue($users, 'address.street');
// using an array of keys to retrieve the value
$value = \yii\helpers\ArrayHelper::getValue($versions, ['1.0', 'date']);
mixed getValue( $array, $key, $default = null ) | ||
$array | array|object | Array or object to extract value from |
$key | string|Closure|array | Key name of the array element, an array of keys or property name of the object,
or an anonymous function returning the value. The anonymous function signature should be:
|
$default | mixed | The default value to be returned if the specified array key does not exist. Not used when getting value from an object. |
return | mixed | The value of the element if found, default value otherwise |
---|---|---|
throws | yii\base\InvalidParamException | if $array is neither an array nor an object. |
Decodes HTML entities into the corresponding characters in an array of strings.
Only array values will be decoded by default. If a value is an array, this method will also decode it recursively. Only string values will be decoded.
See also http://www.php.net/manual/en/function.htmlspecialchars-decode.php.
array htmlDecode( $data, $valuesOnly = true ) | ||
$data | array | Data to be decoded |
$valuesOnly | boolean | Whether to decode array values only. If false, both the array keys and array values will be decoded. |
return | array | The decoded data |
---|
Encodes special characters in an array of strings into HTML entities.
Only array values will be encoded by default. If a value is an array, this method will also encode it recursively. Only string values will be encoded.
See also http://www.php.net/manual/en/function.htmlspecialchars.php.
array htmlEncode( $data, $valuesOnly = true, $charset = null ) | ||
$data | array | Data to be encoded |
$valuesOnly | boolean | Whether to encode array values only. If false, both the array keys and array values will be encoded. |
$charset | string | The charset that the data is using. If not set, yii\base\Application::$charset will be used. |
return | array | The encoded data |
---|
Indexes an array according to a specified key.
The input array should be multidimensional or an array of objects.
The key can be a key name of the sub-array, a property name of object, or an anonymous function which returns the key value given an array element.
If a key value is null, the corresponding array element will be discarded and not put in the result.
For example,
$array = [
['id' => '123', 'data' => 'abc'],
['id' => '345', 'data' => 'def'],
];
$result = ArrayHelper::index($array, 'id');
// the result is:
// [
// '123' => ['id' => '123', 'data' => 'abc'],
// '345' => ['id' => '345', 'data' => 'def'],
// ]
// using anonymous function
$result = ArrayHelper::index($array, function ($element) {
return $element['id'];
});
array index( $array, $key ) | ||
$array | array | The array that needs to be indexed |
$key | string|Closure | The column name or anonymous function whose result will be used to index the array |
return | array | The indexed array |
---|
Returns a value indicating whether the given array is an associative array.
An array is associative if all its keys are strings. If $allStrings
is false,
then an array will be treated as associative if at least one of its keys is a string.
Note that an empty array will NOT be considered associative.
boolean isAssociative( $array, $allStrings = true ) | ||
$array | array | The array being checked |
$allStrings | boolean | Whether the array keys must be all strings in order for the array to be treated as associative. |
return | boolean | Whether the array is associative |
---|
Returns a value indicating whether the given array is an indexed array.
An array is indexed if all its keys are integers. If $consecutive
is true,
then the array keys must be a consecutive sequence starting from 0.
Note that an empty array will be considered indexed.
boolean isIndexed( $array, $consecutive = false ) | ||
$array | array | The array being checked |
$consecutive | boolean | Whether the array keys must be a consecutive sequence in order for the array to be treated as indexed. |
return | boolean | Whether the array is associative |
---|
Checks if the given array contains the specified key.
This method enhances the array_key_exists()
function by supporting case-insensitive
key comparison.
boolean keyExists( $key, $array, $caseSensitive = true ) | ||
$key | string | The key to check |
$array | array | The array with keys to check |
$caseSensitive | boolean | Whether the key comparison should be case-sensitive |
return | boolean | Whether the array contains the specified key |
---|
Builds a map (key-value pairs) from a multidimensional array or an array of objects.
The $from
and $to
parameters specify the key names or property names to set up the map.
Optionally, one can further group the map according to a grouping field $group
.
For example,
$array = [
['id' => '123', 'name' => 'aaa', 'class' => 'x'],
['id' => '124', 'name' => 'bbb', 'class' => 'x'],
['id' => '345', 'name' => 'ccc', 'class' => 'y'],
];
$result = ArrayHelper::map($array, 'id', 'name');
// the result is:
// [
// '123' => 'aaa',
// '124' => 'bbb',
// '345' => 'ccc',
// ]
$result = ArrayHelper::map($array, 'id', 'name', 'class');
// the result is:
// [
// 'x' => [
// '123' => 'aaa',
// '124' => 'bbb',
// ],
// 'y' => [
// '345' => 'ccc',
// ],
// ]
array map( $array, $from, $to, $group = null ) | ||
$array | array | |
$from | string|Closure | |
$to | string|Closure | |
$group | string|Closure |
Merges two or more arrays into one recursively.
If each array has an element with the same string key value, the latter will overwrite the former (different from array_merge_recursive). Recursive merging will be conducted if both arrays have an element of array type and are having the same key. For integer-keyed elements, the elements from the latter array will be appended to the former array.
array merge( $a, $b ) | ||
$a | array | Array to be merged to |
$b | array | Array to be merged from. You can specify additional arrays via third argument, fourth argument etc. |
return | array | The merged array (the original arrays are not changed.) |
---|
Sorts an array of objects or arrays (with the same structure) by one or several keys.
void multisort( <b>&</b>$array, $key, $direction = SORT_ASC, $sortFlag = SORT_REGULAR ) | ||
$array | array | The array to be sorted. The array will be modified after calling this method. |
$key | string|Closure|array | The key(s) to be sorted by. This refers to a key name of the sub-array
elements, a property name of the objects, or an anonymous function returning the values for comparison
purpose. The anonymous function signature should be: |
$direction | integer|array | The sorting direction. It can be either |
$sortFlag | integer|array | The PHP sort flag. Valid values include
|
throws | yii\base\InvalidParamException | if the $direction or $sortFlag parameters do not have correct number of elements as that of $key. |
---|
Removes an item from an array and returns the value. If the key does not exist in the array, the default value will be returned instead.
Usage examples,
// $array = ['type' => 'A', 'options' => [1, 2]];
// working with array
$type = \yii\helpers\ArrayHelper::remove($array, 'type');
// $array content
// $array = ['options' => [1, 2]];
mixed|null remove( <b>&</b>$array, $key, $default = null ) | ||
$array | array | The array to extract value from |
$key | string | Key name of the array element |
$default | mixed | The default value to be returned if the specified key does not exist |
return | mixed|null | The value of the element if found, default value otherwise |
---|
Converts an object or an array of objects into an array.
array toArray( $object, $properties = [], $recursive = true ) | ||
$object | object|array | The object to be converted into an array |
$properties | array | A mapping from object class names to the properties that need to put into the resulting arrays. The properties specified for each class is an array of the following format:
The result of
|
$recursive | boolean | Whether to recursively converts properties which are objects into arrays. |
return | array | The array representation of the object |
---|