Download the PHP package friendly-pixel/ar without Composer
On this page you can find all versions of the php package friendly-pixel/ar. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download friendly-pixel/ar
More information about friendly-pixel/ar
Files in friendly-pixel/ar
Package ar
Short Description Consistent array functions
License MIT
Homepage https://github.com/friendly-pixel/ar/
Informations about the package ar
Ar makes working with PHP arrays easy
- Consistent: All functions accept the array as first parameter.
- Immutable: the input array is never modified. Fluent style returns a new object for every call.
- Tested: unit-tested with 100% code coverage.
- Familiar: function names follow PHP whereever possible.
Fluent style:
Functional style:
Install
Install the latest version using Composer:
Methods
- count()
- filter()
- first()
- flat()
- forEach()
- implode()
- keys()
- last()
- map()
- mapKeys()
- merge()
- push()
- reduce()
- search()
- slice()
- sort()
- splice()
- unique()
- unshift()
- values()
Fluent style only:
- wrap()
- unwrap()
count
Count how many items there are in the array.
filter
Pass every value, key into a user-supplied callable, and only put the item into the result array if the returned value is true
.
Keys are preserved only when array_is_list($array)
returns false;
@template A
@param A[] $array
@param callable(A $value, mixed $key): bool $callable
@return A[]
first
Returns the first value of the array or false
when it's empty.
@template A
@param A[] $array
@return A
flat
The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.
@param int $depth To what level to flatten the array. Default: 1
@return mixed[]
forEach
Walk over every value, key. Pass every value, key into a user-supplied callable.
@template A
@param A[] $array
@param callable(A $value, mixed $key): void $callable
@return A[] Original array, unmodified
implode
Join all values into a big string, using $glue
as separator.
$glue
is optional.
keys
Return the keys of an array as a sequential array.
@return mixed[]
last
Returns the last value of the array or false
when it's empty.
@template A
@param A[] $array
@return A
map
Transform values. Pass every value, key into a user-supplied callable, and put the returned value into the result array. Keys are preserved.
@template A
@template B
@param A[] $array
@param callable(A $value, mixed $key): B $callable
@return B[]
mapKeys
Transform keys. Pass every value, key and key into a user-supplied callable, and use the returned value as key in the result array.
@template A
@template K
@param A[] $array
@param callable(A $value, mixed $key): K $callable
@return array<K, A>
merge
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended. Values in the input arrays with numeric keys will be renumbered with incrementing keys starting from zero in the result array.
@template A
@var A[][] $arrays
@return A[]
push
Append one or more items to the end of array.
@template A
@param A[] $array
@param A[] $values
@return A[]
reduce
Iteratively reduce the array to a single value using a callback function.
@template A
@template B
@param A[] $array
@param callable(B|null $carry, A $value, mixed $key): B $callable
@param B|null $initial If the optional initial is available, it will be used at the beginning of the process, or as a final result in case the array is empty.
@return B
search
Return the first value for which the callable returns true
.
Returns null
otherwise.
@template A
@param A[] $array
@param callable(A $value, mixed $key): bool $callable
@return A|null
slice
Extract a slice of the array, include $length
items, and starting from $offset
.
@template A
@param A[] $array
@param int $offset If offset is non-negative, the sequence will start at that offset in the array. If offset is negative, the sequence will start that far from the end of the array.
@param ?int $length If length is given and is positive, then the sequence will have up to that many elements in it. If the array is shorter than the length, then only the available array elements will be present. If length is given and is negative then the sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from offset up until the end of the array.
@return A[]
sort
Sort an array by values using a user-defined comparison function.
This function assigns new keys to the elements in array. It will remove any existing keys that may have been assigned.
@template A
@param A[] $array
@param callable(A $valueA, A $valueB): int $callable
Return an integer smaller then, equal to,
or larger than 0 to indicate that $valueA is less
then, equal to, or larger than $valueB.
@return A[]
splice
Remove a portion of the array and replace it with something else. Other than the default php function, this returns the changed array, not the extracted elements.
@template A
@param A[] $array
@param int $offset If offset is positive then the start of the removed portion is at that offset from the beginning of the array.
If offset is negative then the start of the removed portion is at that offset from
the end of the array.
@param ?int $length If length is omitted, removes everything from offset to the end of the array. * If length is specified and is positive, then that many elements will be removed.
If length is specified and is negative, then the end of the removed portion will be
that many elements from the end of the array.
If length is specified and is zero, no elements will be removed.
@param A[] $replacement If replacement array is specified, then the removed elements are replaced with elements from this array.
If offset and length are such that nothing is removed, then the elements from the
replacement array are inserted in the place specified by the offset.
*
If replacement is just one element it is not necessary to put array() or square brackets
around it, unless the element is an array itself, an object or null.
Note: Keys in the replacement array are not preserved.
@return A[] Other than the default php function, this returns the changed array, not the extracted elements.
unique
Remove duplicate values from array.
Keys are preserved only when array_is_list($array)
returns false;
@template A
@param A[] $array
@return A[]
unshift
Prepend one or more items to the beginning of array.
@template A
@param A[] $array
@param A[] $values
@return A[]
values
Return the values of an array as a sequential array.
@template A
@param array<mixed, A> $array
@return array<int, A>
Fluent style only methods
wrap
Wrap an array, so you can use fluent syntax to call multiple methods on it.
Use ->unwrap()
at the end if you need a pure array again.
unwrap
Return the underlying array.
toArray
Alias for unwrap()
License
MIT license