메소드 | 설명 | |
---|---|---|
all ( array $elements, callable $predicate ) : boolean | Returns true if every element in array satisfies the predicate. | |
any ( array $elements, callable $predicate ) : boolean | Returns true if at least one element in the array satisfies the predicate. | |
combine ( array $keys, array $values ) : array | Returns a new array with $keys as array keys and $values as array values. | |
concat ( array $arrays ) : array | Merges array of arrays into one array. | |
contains ( array $array, mixed $element ) : boolean |
Returns true if array contains given element. Comparison is based on the following rules:
- same type + same type = strict check
- object + object = loose check
- array + array = goes through all elements and invokes Arrays::contains
- string + integer = loose check
- boolean + string ('true' or 'false') = loose check
- false in other cases
Example:
$result = Arrays::contains(array(1, 2, 3), 2);
Result:
true
|
|
count ( array $elements, callable $predicate ) : integer | Returns the number of elements for which the predicate returns true. | |
filter ( array $elements, callable $function ) : array | This method filters array using function. Result contains all elements for which function returns true. | |
filterByAllowedKeys ( array $elements, array $allowedKeys ) : array | Returns an array containing only the given keys. | |
filterByKeys ( array $elements, callable $predicate ) : array | Filters array by keys using the predicate. | |
filterNotBlank ( array $elements ) : array | This method filter array will remove all values that are blank. | |
find ( array $elements, callable $function ) : mixed | Finds first element in array that is matched by function. | |
findKeyByValue ( array $elements, string $value ) : boolean | integer | string | This method returns a key for the given value. | |
first ( array $elements ) : mixed | This method returns the first value in the given array. | |
firstOrNull ( array $elements ) : mixed | null | This method returns the first value or null if array is empty. | |
flatten ( array $array ) : array | Returns a new array that is a one-dimensional flattening of the given array. | |
flattenKeysRecursively ( array $array ) : array | Returns maps of the flatten keys with corresponding values. | |
getNestedValue ( array $array, array $keys ) : array | mixed | null | Return nested value when found, otherwise return null value. | |
getValue ( array $elements, string | integer $key, mixed | null $default = null ) : mixed | null | Returns the element for the given key or a default value otherwise. | |
groupBy ( array $elements, callable $keyFunction, string | null $orderField = null ) : array | Group elements in array by result of the given function. If $orderField is set grouped elements will be also sorted. | |
hasNestedKey ( array $array, array $keys, null $flags = null ) : boolean | Checks if array has nested key. It's possible to check array with null values using flag Arrays::TREAT_NULL_AS_VALUE. | |
intersect ( array $array1, array $array2 ) : array | Computes the intersection of arrays. | |
isAssociative ( array $array ) : boolean | Checks if the given array is associative. An array is considered associative when it has at least one string key. | |
keyExists ( array $elements, string | integer $key ) : boolean | Checks is key exists in an array. | |
last ( array $elements ) : mixed | This method returns the last value in the given array. | |
map ( array $elements, callable $function ) : array | This method maps array values using the function. | |
mapEntries ( array $elements, callable $function ) : array | This method maps array values using the function which takes key and value as parameters. | |
mapKeys ( array $elements, callable $function ) : array | This method maps array keys using the function. | |
orderBy ( array $elements, string $orderField ) : array | This method sorts elements in array using order field. | |
randElement ( array $elements ) : null | Returns a random element from the given array. | |
recursiveDiff ( array $array1, array $array2 ) : array |
Returns a recursive diff of two arrays
Example:
$array1 = array('a' => array('b' => 'c', 'd' => 'e'), 'f');
$array2 = array('a' => array('b' => 'c'));
$result = Arrays::recursiveDiff($array1, $array2);
Result:
array('a' => array('d' => 'e'), 'f') |
|
reduce ( array $elements, callable $function ) : mixed | Method to reduce an array elements to a string value. | |
removeNestedKey ( array &$array, array $keys, boolean $removeEmptyParents = false ) | Removes nested keys in array. | |
removeNestedValue ( array &$array, array $keys ) | ||
setNestedValue ( array &$array, array $keys, $value ) | Setting nested value. | |
shuffle ( array $array ) : array | Returns shuffled array with retained key association. | |
sort ( array $array, $comparator ) : array | Returns a new array with is sorted using given comparator. | |
toArray ( mixed $element ) : array | Make array from element. Returns the given argument if it's already an array. | |
toMap ( array $elements, callable $keyFunction, callable | null $valueFunction = null ) : array | This method creates associative array using key and value functions on array elements. | |
uniqueBy ( array $elements, $selector ) : array | Removes duplicate values from an array. It uses the given expression to extract value that is compared. |
메소드 | 설명 | |
---|---|---|
_flattenKeyRecursively ( $array, &$result, $parentKey ) |
$array = array(1, 2);
$all = Arrays::all($array, function ($element) {
return $element < 3;
});
Result:
true
$array = array('a', true, 'c');
$any = Arrays::any($array, function ($element) {
return is_bool($element);
});
Result:
true
$keys = array('id', 'name', 'surname');
$values = array(1, 'john', 'smith');
$combined = Arrays::combine($keys, $values);
Result:
Array
(
[id] => 1
[name] => john
[surname] => smith
)
$result = Arrays::concat(array(array(1, 2), array(3, 4)));
Result:
Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
$result = Arrays::contains(array(1, 2, 3), 2);
Result:
true
$array = array(1, 2, 3);
$count = Arrays::count($array, function ($element) {
return $element < 3;
});
Result:
2
$array = array(1, 2, 3, 4);
$result = Arrays::filter($array, function ($value) {
return $value > 2;
});
Result:
Array
(
[2] => 3
[3] => 4
)
$array = array('a' => 1, 'b' => 2, 'c' => 3);
$filtered = Arrays::filterByAllowedKeys($array, array('a', 'b'));
Result:
Array
(
[a] => 1
[b] => 2
)
$array = array('a1' => 1, 'a2' => 2, 'c' => 3);
$filtered = Arrays::filterByKeys($array, function ($elem) {
return $elem[0] == 'a';
});
Result:
Array
(
[a1] => 1
[b2] => 2
)
public static filterNotBlank ( array $elements ) : array | ||
$elements | array | |
리턴 | array |
$array = array(
'k1' => 4,
'k2' => 'd',
'k3' => 0,
9 => 'p'
);
$key = Arrays::findKeyByValue($array, 0);
Result:
k3
$array = array('one', 'two' 'three');
$first = Arrays::first($array);
Result:
one
$array = array();
$return = Arrays::firstOrNull($array);
Result:
null
$array = array(
'names' => array(
'john',
'peter',
'bill'
),
'products' => array(
'cheese',
array(
'natural' => 'milk',
'brie'
)
)
);
$flatten = Arrays::flatten($array);
Result:
Array
(
[0] => john
[1] => peter
[2] => bill
[3] => cheese
[4] => milk
[5] => brie
)
$array = array(
'customer' => array(
'name' => 'Name',
'phone' => '123456789'
),
'other' => array(
'ids_map' => array(
'1qaz' => 'qaz',
'2wsx' => 'wsx'
),
'first' => array(
'second' => array(
'third' => 'some value'
)
)
)
);
$flatten = Arrays::flattenKeysRecursively($array)
Result:
Array
(
[customer.name] => Name
[customer.phone] => 123456789
[other.ids_map.1qaz] => qaz
[other.ids_map.2wsx] => wsx
[other.first.second.third] => some value
)
public static flattenKeysRecursively ( array $array ) : array | ||
$array | array | |
리턴 | array |
$array = array('1' => array('2' => array('3' => 'value')));
$value = Arrays::getNestedValue($array, array('1', '2', '3'));
Result:
value
$array = array('id' => 1, 'name' => 'john');
$value = Arrays::getValue($array, 'name');
Result:
john
Example:
$array = array('id' => 1, 'name' => 'john');
$value = Arrays::getValue($array, 'surname', '--not found--');
Result:
--not found--
$obj1 = new stdClass();
$obj1->name = 'a';
$obj1->description = '1';
$obj2 = new stdClass();
$obj2->name = 'b';
$obj2->description = '2';
$obj3 = new stdClass();
$obj3->name = 'b';
$obj3->description = '3';
$array = array($obj1, $obj2, $obj3);
$grouped = Arrays::groupBy($array, Functions::extractField('name'));
Result:
Array
(
[a] => Array
(
[0] => stdClass Object
(
[name] => a
[description] => 1
)
)
[b] => Array
(
[0] => stdClass Object
(
[name] => b
[description] => 2
)
[1] => stdClass Object
(
[name] => b
[description] => 3
)
)
)
$array = array('1' => array('2' => array('3' => 'value')));
$value = Arrays::hasNestedKey($array, array('1', '2', '3'));
Result:
true
Example with null values:
$array = array('1' => array('2' => array('3' => null)));
$value = Arrays::hasNestedKey($array, array('1', '2', '3'), Arrays::TREAT_NULL_AS_VALUE);
Result:
true
$result = Arrays::isAssociative(array(1, '2', 'abc'));
Result:
FALSE
$result = Arrays::isAssociative(array(1 => 'b', 'a' => 2, 'abc'));
Result:
TRUE
public static isAssociative ( array $array ) : boolean | ||
$array | array | |
리턴 | boolean |
$array = array('id' => 1, 'name' => 'john');
$return = Arrays::keyExists($array, 'name');
Result:
true
$array = array('a', 'b', 'c');
$last = Arrays::last($array);
Result:
c
$array = array('k1', 'k2', 'k3');
$result = Arrays::map($array, function ($value) {
return 'new_' . $value;
});
Result:
Array
(
[0] => new_k1
[1] => new_k2
[2] => new_k3
)
$array = array('a' => '1', 'b' => '2', 'c' => '3');
$result = Arrays::mapEntries($array, function ($key, $value) {
return $key . '_' . $value;
});
Result:
Array
(
[a] => a_1
[b] => b_2
[c] => c_3
)
$array = array(
'k1' => 'v1',
'k2' => 'v2',
'k3' => 'v3'
);
$arrayWithNewKeys = Arrays::mapKeys($array, function ($key) {
return 'new_' . $key;
});
Result:
Array
(
[new_k1] => v1
[new_k2] => v2
[new_k3] => v3
)
$obj1 = new stdClass();
$obj1->name = 'a';
$obj1->description = '1';
$obj2 = new stdClass();
$obj2->name = 'c';
$obj2->description = '2';
$obj3 = new stdClass();
$obj3->name = 'b';
$obj3->description = '3';
$array = array($obj1, $obj2, $obj3);
$sorted = Arrays::orderBy($array, 'name');
Result:
Array
(
[0] => stdClass Object
(
[name] => a
[description] => 1
)
[1] => stdClass Object
(
[name] => b
[description] => 3
)
[2] => stdClass Object
(
[name] => c
[description] => 2
)
)
$array = array('john', 'city', 'small');
$rand = Arrays::randElement($array);
Result: rand element from array public static randElement ( array $elements ) : null | ||
$elements | array | |
리턴 | null |
$array = array('1' => array('2' => array('3' => 'value')));
Arrays::removeNestedKey($array, array('1', '2'));
Result:
Array
(
[1] => Array
(
)
)
public static removeNestedValue ( array &$array, array $keys ) | ||
$array | array | |
$keys | array |
$array = array();
Arrays::setNestedValue($array, array('1', '2', '3'), 'value');
Result:
Array
(
[1] => Array
(
[2] => Array
(
[3] => value
)
)
)
public static setNestedValue ( array &$array, array $keys, $value ) | ||
$array | array | |
$keys | array | |
$value |
$result = Arrays::shuffle(array(1 => 'a', 2 => 'b', 3 => 'c'));
Result:
Array
(
[1] => a
[3] => c
[2] => b
)
Comparator
class (for instance Comparator::natural()
which yields ordering using comparison operators).
Example:
class Foo
{
private $value;
function __construct($value)
{
$this->value = $value;
}
public function getValue()
{
return $this->value;
}
}
$values = array(new Foo(1), new Foo(3), new Foo(2));
$sorted = Arrays::sort($values, Comparator::compareBy('getValue()'));
Result:
Array
(
[0] => class Foo (1) {
private $value => int(1)
}
[1] => class Foo (1) {
private $value => int(2)
}
[2] => class Foo (1) {
private $value => int(3)
}
)
$result = Arrays::toArray('test');
Result:
Array
(
[0] => test
)
$array = range(1, 2);
$map = Arrays::toMap($array, function ($elem) {
return $elem * 10;
}, function ($elem) {
return $elem + 1;
});
Result:
Array
(
[10] => 2
[20] => 3
)