FyreArray is a free, open-source array manipulation library for PHP.
Using Composer
composer require fyre/array
In PHP:
use Fyre\Utility\Arr;
Chunk
Split an array into chunks.
-
$array
is the input array. -
$size
is a number representing the size of each chunk. -
$preserveKeys
is a boolean indicating whether the array keys will be preserved, and will default to false.
$chunk = Arr::chunk($array, $size, $preserveKeys);
Collapse
Recursively replace elements into the first array.
-
$array
is the input array.
All arguments supplied to this method will be replace into the first array.
$collapsed = Arr::collapse($array, ...$replacements);
Column
Get the values from a single column in the input array.
-
$array
is the input array. -
$key
is the column to pull values from.
$column = Arr::column($array, $key);
Combine
Creates an array by using one array for keys and another for its values.
-
$keys
is an array containing the keys. -
$values
is an array containing the values.
$combined = Arr::combine($keys, $values);
Count
Count all elements in an array.
-
$array
is the input array. -
$mode
is an integer representing the counting mode, and will default to Arr::COUNT_NORMAL.
$count = Arr::count($array, $mode);
Difference
Find values in the first array not present in any of the other arrays.
-
$array
is the input array.
Any additional arguments supplied will be used to test for the values of the first array.
$diff = Arr::diff($array, ...$arrays);
Divide
Split an array into keys and values.
-
$array
is the input array.
$divided = Arr::divide($array);
Dot
Flatten a multi-dimensional array using "dot" notation.
-
$array
is the input array. -
$prefix
is a string representing the key prefix, and will default to null.
$dot = Arr::dot($array, $prefix);
Except
Filter an array without the specified key/value pairs.
-
$array
is the input array. -
$keys
is an array containing the keys to remove.
$except = Arr::except($array, $keys);
Fill
Fill an array with values.
-
$amount
is a number representing the number of elements to insert. -
$value
is the value to insert.
$filled = Arr::fill($amount, $value);
Filter
Filter elements of an array using a callback function.
-
$array
is the input array. -
$callback
is the callback function to use. -
$mode
is a number determining representing which arguments will be sent to the callback, and will default to Arr::FILTER_BOTH.
If $callback
is omitted this function will filter empty elements from the array.
$filtered = Arr::filter($array, $callback);
Find
Find the first value in an array that satisfies a callback.
-
$array
is the input array. -
$callback
is the callback function to use. -
$default
is the default value to return, and will default to null.
$find = Arr::find($array, $callback, $default);
Find Last
Find the last value in an array that satisfies a callback.
-
$array
is the input array. -
$callback
is the callback function to use. -
$default
is the default value to return.
$findLast = Arr::findLast($array, $callback, $default);
Flatten
Flatten a multi-dimensional array into a single level.
-
$array
is the input array. -
$maxDepth
is a number representing the maximum depth to flatten, and will default to 1.
$flattened = Arr::flatten($array, $maxDepth);
Forget Dot
Remove a key/value pair using "dot" notation.
-
$array
is the input array. -
$key
is the key to remove.
$forgetDot = Arr::forgetDot($array, $key);
Get Dot
Retrieve a value using "dot" notation.
-
$array
is the input array. -
$key
is the key to lookup. -
$defaultValue
is the default value to return, and will default to null.
$getDot = Arr::getDot($array, $key, $defaultValue);
Has Dot
Determine whether a given element exists in an array using "dot" notation.
-
$array
is the input array. -
$key
is the key to check for.
$hasDot = Arr::hasDot($array, $key);
Has Key
Determine whether a given key exists in an array.
-
$array
is the input array. -
$key
is the key to check for.
$hasKey = Arr::hasKey($array, $key);
Includes
Determine whether a given value exists in an array.
-
$array
is the input array. -
$value
is the value to check for.
$includes = Arr::includes($array, $value);
Index
Index a multi-dimensional array using a given key value.
-
$array
is the input array. -
$key
is the column to pull key values from.
$index = Arr::index($array, $key);
Index Of
Search an array for a given value and returns the first key.
-
$array
is the input array. -
$value
is the value to search for. -
$strict
is a boolean indicating whether a strict search will be performed, and will default to false.
$indexOf = Arr::indexOf($array, $value, $strict);
Intersect
Find values in the first array present in all of the other arrays.
-
$array
is the input array.
All additional arguments supplied will be used to test for intersections.
$intersect = Arr::intersect($array, ...$arrays);
Is Array
Determine whether the value is an array.
-
$value
is the value to test.
$isArray = Arr::isArray($value);
Is List
Determine whether an array has consecutive keys starting from 0.
-
$array
is the array to test.
$isList = Arr::isList($array);
Join
Join an array elements using a specified separator.
-
$array
is the input array. -
$separator
is a string representing the separator to join with, and will default to ","
$joined = Arr::join($array, $separator);
Keys
Get all keys of an array.
-
$array
is the input array.
$keys = Arr::keys($array);
Last Index Of
Search an array for a given value and returns the last key.
-
$array
is the input array. -
$value
is the value to search for. -
$strict
is a boolean indicating whether a strict search will be performed, and will default to false.
$lastIndexOf = Arr::lastIndexOf($array, $value, $strict);
Map
Apply a callback to the elements of an array.
-
$array
is the input array. -
$callback
is the callback function to use.
$map = Arr::map($array, $callback);
Merge
Merge one or more arrays.
-
$array
is the input array.
All additional arguments supplied will be merged with the first array.
$merged = Arr::merge($array, ...$arrays);
Only
Filter an array with only the specified key/value pairs.
-
$array
is the input array. -
$keys
is an array containing the keys to include.
$only = Arr::only($array, $keys);
Pad
Pad an array to a specified length with a value.
-
$array
is the input array. -
$size
is a number representing the new size of the array. -
$value
is the value to pad with.
$padded = Arr::pad($array, $size, $value);
Pluck Dot
Pluck a list of values using "dot" notation.
-
$arrays
is the input arrays. -
$key
is the key to lookup.
$values = Arr::pluckDot($arrays, $key);
Pop
Pop the element off the end of array.
-
$array
is the input array.
$pop = Arr::pop($array);
Push
Push one or more elements onto the end of array.
-
$array
is the input array.
All additional arguments supplied will be pushed onto the array.
$newCount = Arr::push($array, ...$values);
Random Value
Get a random value from an array.
-
$array
is the input array.
$randomValue = Arr::randomValue($array);
Range
Create an array containing a range of elements.
-
$start
is a number representing the first value of the sequence. -
$end
is a number representing the ending value in the sequence. -
$step
is a number representing the increment between values in the sequence, and will default to 1.
$range = Arr::range($start, $end, $step);
Reduce
Iteratively reduce an array to a single value using a callback function.
-
$array
is the input array. -
$callback
is the callback function to use. -
$initial
is the initial value to use, and will default to null.
$reduced = Arr::reduce($array, $callback, $initial);
Reverse
Reverse the order of elements in an array.
-
$array
is the input array.
$reversed = Arr::reverse($array);
Set Dot
Set a value using "dot" notation.
-
$array
is the input array. -
$key
is the key. -
$value
is the value to set. -
$overwrite
is a boolean indicating whether previous values will be overwritten, and will default to true.
$setDot = Arr::setDot($array, $key, $value, $overwrite);
Shift
Shift an element off the beginning of the array.
-
$array
is the input array.
$shift = Arr::shift($array);
Shuffle
Shuffle an array.
-
$array
is the input array.
$shuffled = Arr::shuffle($array);
Slice
Extract a slice of the array.
-
$array
is the input array. -
$offset
is a number representing the starting offset. -
$length
is a number representing the length of the slice. -
$preserveKeys
determines if the array keys will be preserved, and will default to false.
$slice = Arr::slice($array, $offset, $length, $preserveKeys);
Sort
Sort an array.
-
$array
is the input array. -
$sort
is a number representing the sorting flag or a comparison Closure, and will default to Arr::SORT_NATURAL.
$sorted = Arr::sort($array, $sort);
Splice
Remove a portion of the array and replace it with something else.
-
$array
is the input array. -
$offset
is a number representing the starting offset. -
$length
is a number representing the length of the slice. -
$replacement
is the element(s) to insert in the array.
$spliced = Arr::splice($array, $offset, $length, $replacement);
Unique
Remove duplicate values from an array.
-
$array
is the input array.
$unique = Arr::unique($array);
Unshift
Prepend one or more elements to the beginning of an array.
-
$array
is the input array.
All additional arguments supplied will be prepended to the array.
$newCount = Arr::unshift($array, ...$values);
Values
Get all values of an array.
-
$array
is the input array.
$values = Arr::values($array);
Wrap
Create an array from any value.
-
$value
is the value to wrap.
$wrapped = Arr::wrap($value);