PHP Class yii\helpers\BaseArrayHelper

Do not use BaseArrayHelper. Use [[ArrayHelper]] instead.
Since: 2.0
Author: Qiang Xue ([email protected])
Afficher le fichier Open project: yiisoft/yii2 Class Usage Examples

Méthodes publiques

Méthode Description
filter ( array $array, array $filters ) : array Filters array according to rules specified.
getColumn ( array $array, string | Closure $name, boolean $keepKeys = true ) : array Returns the values of a specified column in an array.
getValue ( array | object $array, string | Closure | array $key, mixed $default = null ) : mixed Retrieves the value of an array element or object property with the given key or property name.
htmlDecode ( array $data, boolean $valuesOnly = true ) : array Decodes HTML entities into the corresponding characters in an array of strings.
htmlEncode ( array $data, boolean $valuesOnly = true, string $charset = null ) : array Encodes special characters in an array of strings into HTML entities.
index ( array $array, string | Closure | null $key, string | string[] | Closure[] | null $groups = [] ) : array Indexes and/or groups the array according to a specified key.
isAssociative ( array $array, boolean $allStrings = true ) : boolean Returns a value indicating whether the given array is an associative array.
isIn ( mixed $needle, array | Traversable $haystack, boolean $strict = false ) : boolean Check whether an array or [[\Traversable]] contains an element.
isIndexed ( array $array, boolean $consecutive = false ) : boolean Returns a value indicating whether the given array is an indexed array.
isSubset ( array | Traversable $needles, array | Traversable $haystack, boolean $strict = false ) : boolean Checks whether an array or [[\Traversable]] is a subset of another array or [[\Traversable]].
isTraversable ( mixed $var ) : boolean Checks whether a variable is an array or [[\Traversable]].
keyExists ( string $key, array $array, boolean $caseSensitive = true ) : boolean Checks if the given array contains the specified key.
map ( array $array, string | Closure $from, string | Closure $to, string | Closure $group = null ) : array Builds a map (key-value pairs) from a multidimensional array or an array of objects.
merge ( array $a, array $b ) : array Merges two or more arrays into one recursively.
multisort ( array &$array, string | Closure | array $key, integer | array $direction = SORT_ASC, integer | array $sortFlag = SORT_REGULAR ) Sorts an array of objects or arrays (with the same structure) by one or several keys.
remove ( array &$array, string $key, mixed $default = null ) : mixed | null 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.
removeValue ( array &$array, string $value ) : array Removes items with matching values from the array and returns the removed items.
toArray ( object | array | string $object, array $properties = [], boolean $recursive = true ) : array Converts an object or an array of objects into an array.

Method Details

filter() public static méthode

For example: php $array = [ 'A' => [1, 2], 'B' => [ 'C' => 1, 'D' => 2, ], 'E' => 1, ]; $result = \yii\helpers\ArrayHelper::filter($array, ['A']); $result will be: [ 'A' => [1, 2], ] $result = \yii\helpers\ArrayHelper::filter($array, ['A', 'B.C']); $result will be: [ 'A' => [1, 2], 'B' => ['C' => 1], ] $result = \yii\helpers\ArrayHelper::filter($array, ['B', '!B.C']); $result will be: [ 'B' => ['D' => 2], ]
Since: 2.0.9
public static filter ( array $array, array $filters ) : array
$array array Source array
$filters array Rules that define array keys which should be left or removed from results. Each rule is: - `var` - `$array['var']` will be left in result. - `var.key` = only `$array['var']['key'] will be left in result. - `!var.key` = `$array['var']['key'] will be removed from result.
Résultat array Filtered array

getColumn() public static méthode

The input array should be multidimensional or an array of objects. For example, php $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']; });
public static getColumn ( array $array, string | Closure $name, boolean $keepKeys = true ) : array
$array array
$name string | Closure
$keepKeys boolean whether to maintain the array keys. If false, the resulting array will be re-indexed with integers.
Résultat array the list of column values

getValue() public static méthode

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, php 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']);
public static getValue ( array | object $array, string | Closure | array $key, mixed $default = null ) : mixed
$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: `function($array, $defaultValue)`. The possibility to pass an array of keys is available since version 2.0.4.
$default mixed the default value to be returned if the specified array key does not exist. Not used when getting value from an object.
Résultat mixed the value of the element if found, default value otherwise

htmlDecode() public static méthode

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
public static htmlDecode ( array $data, boolean $valuesOnly = true ) : array
$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.
Résultat array the decoded data

htmlEncode() public static méthode

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
public static htmlEncode ( array $data, boolean $valuesOnly = true, string $charset = null ) : array
$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.
Résultat array the encoded data

index() public static méthode

The input should be either multidimensional array or an array of objects. The $key can be either a key name of the sub-array, a property name of object, or an anonymous function that must return the value that will be used as a key. $groups is an array of keys, that will be used to group the input array into one or more sub-arrays based on keys specified. If the $key is specified as null or a value of an element corresponding to the key is null in addition to $groups not specified then the element is discarded. For example: php $array = [ ['id' => '123', 'data' => 'abc', 'device' => 'laptop'], ['id' => '345', 'data' => 'def', 'device' => 'tablet'], ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'], ]; $result = ArrayHelper::index($array, 'id'); The result will be an associative array, where the key is the value of id attribute php [ '123' => ['id' => '123', 'data' => 'abc', 'device' => 'laptop'], '345' => ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'] The second element of an original array is overwritten by the last element because of the same id ] An anonymous function can be used in the grouping array as well. php $result = ArrayHelper::index($array, function ($element) { return $element['id']; }); Passing id as a third argument will group $array by id: php $result = ArrayHelper::index($array, null, 'id'); The result will be a multidimensional array grouped by id on the first level, by device on the second level and indexed by data on the third level: php [ '123' => [ ['id' => '123', 'data' => 'abc', 'device' => 'laptop'] ], '345' => [ // all elements with this index are present in the result array ['id' => '345', 'data' => 'def', 'device' => 'tablet'], ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'], ] ] The anonymous function can be used in the array of grouping keys as well: php $result = ArrayHelper::index($array, 'data', [function ($element) { return $element['id']; }, 'device']); The result will be a multidimensional array grouped by id on the first level, by the device on the second one and indexed by the data on the third level: php [ '123' => [ 'laptop' => [ 'abc' => ['id' => '123', 'data' => 'abc', 'device' => 'laptop'] ] ], '345' => [ 'tablet' => [ 'def' => ['id' => '345', 'data' => 'def', 'device' => 'tablet'] ], 'smartphone' => [ 'hgi' => ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'] ] ] ]
public static index ( array $array, string | Closure | null $key, string | string[] | Closure[] | null $groups = [] ) : array
$array array the array that needs to be indexed or grouped
$key string | Closure | null the column name or anonymous function which result will be used to index the array
$groups string | string[] | Closure[] | null the array of keys, that will be used to group the input array by one or more keys. If the $key attribute or its value for the particular element is null and $groups is not defined, the array element will be discarded. Otherwise, if $groups is specified, array element will be added to the result array without any key. This parameter is available since version 2.0.8.
Résultat array the indexed and/or grouped array

isAssociative() public static méthode

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.
public static isAssociative ( array $array, boolean $allStrings = true ) : boolean
$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.
Résultat boolean whether the array is associative

isIn() public static méthode

This method does the same as the PHP function in_array() but additionally works for objects that implement the [[\Traversable]] interface.
See also: http://php.net/manual/en/function.in-array.php
Since: 2.0.7
public static isIn ( mixed $needle, array | Traversable $haystack, boolean $strict = false ) : boolean
$needle mixed The value to look for.
$haystack array | Traversable The set of values to search.
$strict boolean Whether to enable strict (`===`) comparison.
Résultat boolean `true` if `$needle` was found in `$haystack`, `false` otherwise.

isIndexed() public static méthode

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.
public static isIndexed ( array $array, boolean $consecutive = false ) : boolean
$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.
Résultat boolean whether the array is associative

isSubset() public static méthode

This method will return true, if all elements of $needles are contained in $haystack. If at least one element is missing, false will be returned.
Since: 2.0.7
public static isSubset ( array | Traversable $needles, array | Traversable $haystack, boolean $strict = false ) : boolean
$needles array | Traversable The values that must **all** be in `$haystack`.
$haystack array | Traversable The set of value to search.
$strict boolean Whether to enable strict (`===`) comparison.
Résultat boolean `true` if `$needles` is a subset of `$haystack`, `false` otherwise.

isTraversable() public static méthode

This method does the same as the PHP function is_array() but additionally works on objects that implement the [[\Traversable]] interface.
See also: http://php.net/manual/en/function.is_array.php
Since: 2.0.8
public static isTraversable ( mixed $var ) : boolean
$var mixed The variable being evaluated.
Résultat boolean whether $var is array-like

keyExists() public static méthode

This method enhances the array_key_exists() function by supporting case-insensitive key comparison.
public static keyExists ( string $key, array $array, boolean $caseSensitive = true ) : boolean
$key string the key to check
$array array the array with keys to check
$caseSensitive boolean whether the key comparison should be case-sensitive
Résultat boolean whether the array contains the specified key

map() public static méthode

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, php $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', ], ]
public static map ( array $array, string | Closure $from, string | Closure $to, string | Closure $group = null ) : array
$array array
$from string | Closure
$to string | Closure
$group string | Closure
Résultat array

merge() public static méthode

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. You can use [[UnsetArrayValue]] object to unset value from previous array or [[ReplaceArrayValue]] to force replace former value instead of recursive merging.
public static merge ( array $a, array $b ) : array
$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.
Résultat array the merged array (the original arrays are not changed.)

multisort() public static méthode

Sorts an array of objects or arrays (with the same structure) by one or several keys.
public static multisort ( array &$array, string | Closure | array $key, integer | array $direction = SORT_ASC, integer | array $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: `function($item)`. To sort by multiple keys, provide an array of keys here.
$direction integer | array the sorting direction. It can be either `SORT_ASC` or `SORT_DESC`. When sorting by multiple keys with different sorting directions, use an array of sorting directions.
$sortFlag integer | array the PHP sort flag. Valid values include `SORT_REGULAR`, `SORT_NUMERIC`, `SORT_STRING`, `SORT_LOCALE_STRING`, `SORT_NATURAL` and `SORT_FLAG_CASE`. Please refer to [PHP manual](http://php.net/manual/en/function.sort.php) for more details. When sorting by multiple keys with different sort flags, use an array of sort flags.

remove() public static méthode

Usage examples, php $array = ['type' => 'A', 'options' => [1, 2]]; working with array $type = \yii\helpers\ArrayHelper::remove($array, 'type'); $array content $array = ['options' => [1, 2]];
public static remove ( array &$array, string $key, mixed $default = null ) : mixed | 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
Résultat mixed | null the value of the element if found, default value otherwise

removeValue() public static méthode

Example, php $array = ['Bob' => 'Dylan', 'Michael' => 'Jackson', 'Mick' => 'Jagger', 'Janet' => 'Jackson']; $removed = \yii\helpers\ArrayHelper::removeValue($array, 'Jackson'); result: $array = ['Bob' => 'Dylan', 'Mick' => 'Jagger']; $removed = ['Michael' => 'Jackson', 'Janet' => 'Jackson'];
Since: 2.0.11
public static removeValue ( array &$array, string $value ) : array
$array array the array where to look the value from
$value string the value to remove from the array
Résultat array the items that were removed from the array

toArray() public static méthode

Converts an object or an array of objects into an array.
public static toArray ( object | array | string $object, array $properties = [], boolean $recursive = true ) : array
$object object | array | string 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: ```php [ 'app\models\Post' => [ 'id', 'title', // the key name in array result => property name 'createTime' => 'created_at', // the key name in array result => anonymous function 'length' => function ($post) { return strlen($post->content); }, ], ] ``` The result of `ArrayHelper::toArray($post, $properties)` could be like the following: ```php [ 'id' => 123, 'title' => 'test', 'createTime' => '2013-01-01 12:00AM', 'length' => 301, ] ```
$recursive boolean whether to recursively converts properties which are objects into arrays.
Résultat array the array representation of the object