A
in package
The `A` class provides a set of handy methods to simplify array handling and make it more consistent. The class contains methods for fetching elements from arrays, merging and sorting or shuffling arrays.
Table of Contents
- MERGE_APPEND = 1
- MERGE_OVERWRITE = 0
- MERGE_REPLACE = 2
- append() : array<string|int, mixed>
- Appends the given array
- apply() : array<string|int, mixed>
- Recursively loops through the array and resolves any item defined as `Closure`, applying the passed parameters
- average() : float
- Returns the average value of an array
- extend() : array<string|int, mixed>
- Merges arrays recursively
- fill() : array<string|int, mixed>
- Fills an array up with additional elements to certain amount.
- filter() : array<string|int, mixed>
- Filter the array using the given callback using both value and key
- first() : mixed
- Returns the first element of an array
- get() : mixed
- Gets an element of an array by key
- isAssociative() : bool
- Checks whether an array is associative or not
- join() : string
- last() : mixed
- Returns the last element of an array
- map() : array<string|int, mixed>
- A simple wrapper around array_map with a sane argument order
- merge() : array<string|int, mixed>
- Merges arrays recursively
- missing() : array<string|int, mixed>
- Checks for missing elements in an array
- move() : array<string|int, mixed>
- Move an array item to a new index
- nest() : array<string|int, mixed>
- Normalizes an array into a nested form by converting dot notation in keys to nested structures
- nestByKeys() : array<string|int, mixed>|mixed
- Recursively creates a nested array from a set of keys with a key on each level
- pluck() : array<string|int, mixed>
- Plucks a single column from an array
- prepend() : array<string|int, mixed>
- Prepends the given array
- random() : array<string|int, mixed>
- Returns a number of random elements from an array, either in original or shuffled order
- shuffle() : array<string|int, mixed>
- Shuffles an array and keeps the keys
- sort() : array<string|int, mixed>
- Sorts a multi-dimensional array by a certain column
- update() : array<string|int, mixed>
- Update an array with a second array The second array can contain callbacks as values, which will get the original values as argument
- without() : array<string|int, mixed>
- Remove key(s) from an array
- wrap() : array<string|int, mixed>
- Wraps the given value in an array if it's not an array yet.
Constants
MERGE_APPEND
public
mixed
MERGE_APPEND
= 1
MERGE_OVERWRITE
public
mixed
MERGE_OVERWRITE
= 0
MERGE_REPLACE
public
mixed
MERGE_REPLACE
= 2
Methods
append()
Appends the given array
public
static append(array<string|int, mixed> $array, array<string|int, mixed> $append) : array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
- $append : array<string|int, mixed>
Return values
array<string|int, mixed> —apply()
Recursively loops through the array and resolves any item defined as `Closure`, applying the passed parameters
public
static apply(array<string|int, mixed> $array, mixed ...$args) : array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
- $args : mixed
-
Parameters to pass to the closures
Tags
Return values
array<string|int, mixed> —average()
Returns the average value of an array
public
static average(array<string|int, mixed> $array, int $decimals) : float
Parameters
- $array : array<string|int, mixed>
-
The source array
- $decimals : int
-
The number of decimals to return
Return values
float —The average value
extend()
Merges arrays recursively
public
static extend(array<string|int, mixed> ...$arrays) : array<string|int, mixed>
$defaults = [
'username' => 'admin',
'password' => 'admin',
];
$options = A::extend($defaults, ['password' => 'super-secret']);
// returns: [
// 'username' => 'admin',
// 'password' => 'super-secret'
// ];
Parameters
- $arrays : array<string|int, mixed>
Return values
array<string|int, mixed> —fill()
Fills an array up with additional elements to certain amount.
public
static fill(array<string|int, mixed> $array, int $limit[, mixed $fill = 'placeholder' ]) : array<string|int, mixed>
$array = [
'cat' => 'miao',
'dog' => 'wuff',
'bird' => 'tweet'
];
$result = A::fill($array, 5, 'elephant');
// result: [
// 'cat',
// 'dog',
// 'bird',
// 'elephant',
// 'elephant',
// ];
Parameters
- $array : array<string|int, mixed>
-
The source array
- $limit : int
-
The number of elements the array should contain after filling it up.
- $fill : mixed = 'placeholder'
-
The element, which should be used to fill the array
Return values
array<string|int, mixed> —The filled-up result array
filter()
Filter the array using the given callback using both value and key
public
static filter(array<string|int, mixed> $array, callable $callback) : array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
- $callback : callable
Tags
Return values
array<string|int, mixed> —first()
Returns the first element of an array
public
static first(array<string|int, mixed> $array) : mixed
$array = [
'cat' => 'miao',
'dog' => 'wuff',
'bird' => 'tweet'
];
$first = A::first($array);
// first: 'miao'
Parameters
- $array : array<string|int, mixed>
-
The source array
Return values
mixed —The first element
get()
Gets an element of an array by key
public
static get(array<string|int, mixed> $array, mixed $key[, mixed $default = null ]) : mixed
$array = [
'cat' => 'miao',
'dog' => 'wuff',
'bird' => 'tweet'
];
echo A::get($array, 'cat');
// output: 'miao'
echo A::get($array, 'elephant', 'shut up');
// output: 'shut up'
$catAndDog = A::get($array, ['cat', 'dog']);
// result: ['cat' => 'miao', 'dog' => 'wuff'];
Parameters
- $array : array<string|int, mixed>
-
The source array
- $key : mixed
-
The key to look for
- $default : mixed = null
-
Optional default value, which should be returned if no element has been found
Return values
mixed —isAssociative()
Checks whether an array is associative or not
public
static isAssociative(array<string|int, mixed> $array) : bool
$array = ['a', 'b', 'c'];
A::isAssociative($array);
// returns: false
$array = ['a' => 'a', 'b' => 'b', 'c' => 'c'];
A::isAssociative($array);
// returns: true
Parameters
- $array : array<string|int, mixed>
-
The array to analyze
Return values
bool —true: The array is associative false: It's not
join()
public
static join(mixed $value[, mixed $separator = ', ' ]) : string
Parameters
- $value : mixed
- $separator : mixed = ', '
Return values
string —last()
Returns the last element of an array
public
static last(array<string|int, mixed> $array) : mixed
$array = [
'cat' => 'miao',
'dog' => 'wuff',
'bird' => 'tweet'
];
$last = A::last($array);
// last: 'tweet'
Parameters
- $array : array<string|int, mixed>
-
The source array
Return values
mixed —The last element
map()
A simple wrapper around array_map with a sane argument order
public
static map(array<string|int, mixed> $array, callable $map) : array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
- $map : callable
Tags
Return values
array<string|int, mixed> —merge()
Merges arrays recursively
public
static merge(array<string|int, mixed> $array1, array<string|int, mixed> $array2[, int $mode = A::MERGE_APPEND ]) : array<string|int, mixed>
Parameters
- $array1 : array<string|int, mixed>
- $array2 : array<string|int, mixed>
- $mode : int = A::MERGE_APPEND
-
Behavior for elements with numeric keys; A::MERGE_APPEND: elements are appended, keys are reset; A::MERGE_OVERWRITE: elements are overwritten, keys are preserved A::MERGE_REPLACE: non-associative arrays are completely replaced
Return values
array<string|int, mixed> —missing()
Checks for missing elements in an array
public
static missing(array<string|int, mixed> $array[, array<string|int, mixed> $required = [] ]) : array<string|int, mixed>
This is very handy to check for missing user values in a request for example.
$array = [
'cat' => 'miao',
'dog' => 'wuff',
'bird' => 'tweet'
];
$required = ['cat', 'elephant'];
$missing = A::missing($array, $required);
// missing: [
// 'elephant'
// ];
Parameters
- $array : array<string|int, mixed>
-
The source array
- $required : array<string|int, mixed> = []
-
An array of required keys
Return values
array<string|int, mixed> —An array of missing fields. If this is empty, nothing is missing.
move()
Move an array item to a new index
public
static move(array<string|int, mixed> $array, int $from, int $to) : array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
- $from : int
- $to : int
Return values
array<string|int, mixed> —nest()
Normalizes an array into a nested form by converting dot notation in keys to nested structures
public
static nest(array<string|int, mixed> $array[, array<string|int, mixed> $ignore = [] ]) : array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
- $ignore : array<string|int, mixed> = []
-
List of keys in dot notation that should not be converted to a nested structure
Return values
array<string|int, mixed> —nestByKeys()
Recursively creates a nested array from a set of keys with a key on each level
public
static nestByKeys(mixed $value, array<string|int, mixed> $keys) : array<string|int, mixed>|mixed
Parameters
- $value : mixed
-
Arbitrary value that will end up at the bottom of the tree
- $keys : array<string|int, mixed>
-
List of keys to use sorted from the topmost level
Return values
array<string|int, mixed>|mixed —Nested array or (if $keys
is empty) the input $value
pluck()
Plucks a single column from an array
public
static pluck(array<string|int, mixed> $array, string $key) : array<string|int, mixed>
$array[] = [
'id' => 1,
'username' => 'homer',
];
$array[] = [
'id' => 2,
'username' => 'marge',
];
$array[] = [
'id' => 3,
'username' => 'lisa',
];
var_dump(A::pluck($array, 'username'));
// result: ['homer', 'marge', 'lisa'];
Parameters
- $array : array<string|int, mixed>
-
The source array
- $key : string
-
The key name of the column to extract
Return values
array<string|int, mixed> —The result array with all values from that column.
prepend()
Prepends the given array
public
static prepend(array<string|int, mixed> $array, array<string|int, mixed> $prepend) : array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
- $prepend : array<string|int, mixed>
Return values
array<string|int, mixed> —random()
Returns a number of random elements from an array, either in original or shuffled order
public
static random(array<string|int, mixed> $array[, int $count = 1 ][, bool $shuffle = false ]) : array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
- $count : int = 1
- $shuffle : bool = false
Return values
array<string|int, mixed> —shuffle()
Shuffles an array and keeps the keys
public
static shuffle(array<string|int, mixed> $array) : array<string|int, mixed>
$array = [
'cat' => 'miao',
'dog' => 'wuff',
'bird' => 'tweet'
];
$shuffled = A::shuffle($array);
// output: [
// 'dog' => 'wuff',
// 'cat' => 'miao',
// 'bird' => 'tweet'
// ];
Parameters
- $array : array<string|int, mixed>
-
The source array
Return values
array<string|int, mixed> —The shuffled result array
sort()
Sorts a multi-dimensional array by a certain column
public
static sort(array<string|int, mixed> $array, string $field[, string $direction = 'desc' ][, int $method = SORT_REGULAR ]) : array<string|int, mixed>
$array[0] = [
'id' => 1,
'username' => 'mike',
];
$array[1] = [
'id' => 2,
'username' => 'peter',
];
$array[3] = [
'id' => 3,
'username' => 'john',
];
$sorted = A::sort($array, 'username ASC');
// Array
// (
// [0] => Array
// (
// [id] => 3
// [username] => john
// )
// [1] => Array
// (
// [id] => 1
// [username] => mike
// )
// [2] => Array
// (
// [id] => 2
// [username] => peter
// )
// )
Parameters
- $array : array<string|int, mixed>
-
The source array
- $field : string
-
The name of the column
- $direction : string = 'desc'
-
desc (descending) or asc (ascending)
- $method : int = SORT_REGULAR
-
A PHP sort method flag or 'natural' for natural sorting, which is not supported in PHP by sort flags
Return values
array<string|int, mixed> —The sorted array
update()
Update an array with a second array The second array can contain callbacks as values, which will get the original values as argument
public
static update(array<string|int, mixed> $array, array<string|int, mixed> $update) : array<string|int, mixed>
$user = [
'username' => 'homer',
'email' => 'homer@simpsons.com'
];
// simple updates
A::update($user, [
'username' => 'homer j. simpson'
]);
// with callback
A::update($user, [
'username' => function ($username) {
return $username . ' j. simpson'
}
]);
Parameters
- $array : array<string|int, mixed>
- $update : array<string|int, mixed>
Return values
array<string|int, mixed> —without()
Remove key(s) from an array
public
static without(array<string|int, mixed> $array, int|string|array<string|int, mixed> $keys) : array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
- $keys : int|string|array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —wrap()
Wraps the given value in an array if it's not an array yet.
public
static wrap([mixed|null $array = null ]) : array<string|int, mixed>
Parameters
- $array : mixed|null = null