Class craft\helpers\ArrayHelper
- Inheritance
- craft\helpers\ArrayHelper » yii\helpers\ArrayHelper » yii\helpers\BaseArrayHelper
- Available since version
- 3.0
- Source Code
- https://github.com/craftcms/cms/blob/master/src/helpers/ArrayHelper.php
Class ArrayHelper
Method | Description | Defined By |
---|---|---|
filter() |
Filters array according to rules specified. | yii\helpers\BaseArrayHelper |
filterByValue() |
Filters an array to only the values where a given key (the name of a sub-array key or sub-object property) is set to a given value. | craft\helpers\ArrayHelper |
filterEmptyStringsFromArray() |
Filters empty strings from an array. | craft\helpers\ArrayHelper |
firstKey() |
Returns the first key in a given array. | craft\helpers\ArrayHelper |
firstValue() |
Returns the first value in a given array. | craft\helpers\ArrayHelper |
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 and/or groups the 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 |
isIn() |
Check whether an array or Traversable contains an element. | yii\helpers\BaseArrayHelper |
isIndexed() |
Returns a value indicating whether the given array is an indexed array. | yii\helpers\BaseArrayHelper |
isSubset() |
Checks whether an array or Traversable is a subset of another array or Traversable. | yii\helpers\BaseArrayHelper |
isTraversable() |
Checks whether a variable is an array or Traversable. | 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 |
prependOrAppend() |
Prepends or appends a value to an array. | craft\helpers\ArrayHelper |
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 |
removeValue() |
Removes items with matching values from the array and returns the removed items. | yii\helpers\BaseArrayHelper |
rename() |
Renames an item in an array. If the new key already exists in the array and the old key doesn’t, the array will be left unchanged. | craft\helpers\ArrayHelper |
setValue() |
Writes a value into an associative array at the key path specified. | yii\helpers\BaseArrayHelper |
toArray() |
Converts an object or an array of objects into an array. | craft\helpers\ArrayHelper |
Method Details
filterByValue()
public static method
#
Filters an array to only the values where a given key (the name of a sub-array key or sub-object property) is set to a given value.
Array keys are preserved.
public static array filterByValue ( $array, $key, $value, \craft\helpers\bool $strict = false )
$array |
array, Traversable | The array that needs to be indexed or grouped |
$key |
string, Closure | The column name or anonymous function which result will be used to index the array |
$value |
mixed | The value that $key should be compared with |
$strict |
boolean | Whether a strict type comparison should be used when checking array element values against $value |
return | array | The filtered array |
---|
filterEmptyStringsFromArray()
public static method
#
Filters empty strings from an array.
public static array filterEmptyStringsFromArray ( array $arr )
$arr |
array |
firstKey()
public static method
#
Returns the first key in a given array.
public static string, integer, null firstKey ( array $arr )
$arr |
array | |
return | string, integer, null | The first key, whether that is a number (if the array is numerically indexed) or a string, or null if $arr isn’t an array, or is empty. |
---|
firstValue()
public static method
#
Returns the first value in a given array.
public static mixed firstValue ( array $arr )
$arr |
array | |
return | mixed | The first value, or null if $arr isn’t an array, or is empty. |
---|
prependOrAppend()
public static method
#
Prepends or appends a value to an array.
public static void prependOrAppend ( array &$array, $value, \craft\helpers\bool $prepend )
$array |
||
$value |
mixed | The value to prepend/append to the array |
$prepend |
boolean | true will prepend the value; false will append it
|
rename()
public static method
#
Renames an item in an array. If the new key already exists in the array and the old key doesn’t, the array will be left unchanged.
public static void rename ( array &$array, \craft\helpers\string $oldKey, \craft\helpers\string $newKey, $default = null )
$array |
array | The array to extract value from |
$oldKey |
string | Old key name of the array element |
$newKey |
string | New key name of the array element |
$default |
mixed | The default value to be set if the specified old key does not exist |
toArray()
public static method
#
Converts an object or an array of objects into an array.
public static array toArray ( $object, $properties = [], $recursive = true )
$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:
[
'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:
[
'id' => 123,
'title' => 'test',
'createTime' => '2013-01-01 12:00AM',
'length' => 301,
]
|
$recursive |
boolean | Whether to recursively converts properties which are objects into arrays. |
return | array | The array representation of the object |
---|