<?php
// Accept non-reference array
Arrays::each([1, 2, 3], function(){});
// Accept user defined / built-in functions
Arrays::each([1, 2, 3], "\\var_dump");
Arrays::each([1, 2, 3], [$this, "foo"]);
// Chain of calls
$res = Arrays::chain([1, 2, 3])
->map(function($num){ return $num + 1; })
->filter(function($num){ return $num > 1; })
->reduce(function($carry, $num){ return $carry + $num; }, 0)
->value();-
JavaScript-inspired methods
-
Underscore.js-inspired methods
- Collections
- Arrays
- Objects
- Chaining
-
Other methods
Methods
_.functions,_.extendOwn,_.cloneare not implemented. Given we consider here JavaScript object as associative array, they are not relevant.
Method
_.isElementdepends on DOM and therefore is not applicable in PHP context.
Methods
_.isArguments,_.isRegExp,_.isUndefinedare not relevant in PHP context
Methods
_.isFunction,_.isString,_.isNumber,_.isFinite,_.isNaN,_.isBoolean,_.isDate,_.isError,_.isNullbelong to the corresponded classesDsheiko\Extras\Functions,Dsheiko\Extras\Strings,Dsheiko\Extras\Numbers,Dsheiko\Extras\Booleans,Dsheiko\Extras\Any
Copy the values of all properties from one or more target arrays to a target array.
- see also.
The method works pretty much like
array_mergeexcept it treats consistently associative arrays with numeric keys
{array} $array- target array...$sources- source one or more arrays
assign(array $array, ...$sources): array<?php
$res = Arrays::assign(["foo" => 1, "bar" => 2], ["bar" => 3], ["foo" => 4], ["baz" => 5]);
// $res === ["foo" => 4, "bar" => 3, "baz" => 5]
$res = Arrays::assign([10 => "foo", 20 => "bar"], [20 => "baz"]);
// $res === [ 10 => "foo", 20 => "baz" ]Merge two or more arrays
{array} $array- source array{array} ...$targets- arrays to merge
concat(array $array, array ...$targets): array<?php
$res = Arrays::concat([1, 2], [3, 4], [5, 6]); // [1, 2, 3, 4, 5, 6]Shallow copy part of an array to another location in the same array and returns it, without modifying its size.
{array} $array- source array{int} $targetIndex- zero based index at which to copy the sequence to{int} $beginIndex- (optional) zero based index at which to start copying elements from{int} $endIndex- (optional) zero based index at which to end copying elements from
copyWithin(
array $array,
int $targetIndex,
int $beginIndex = 0,
int $endIndex = null
): array<?php
$res = Arrays::copyWithin([1, 2, 3, 4, 5], 0, 3, 4); // [4, 2, 3, 4, 5]Iterate over a list of elements, yielding each in turn to an iteratee function
{array} $array- source array{mixed} $mixed- iteratee callback
{mixed} $value- element value{int} $index- zero-based element index{array} $array- source array
each(array $array, mixed $mixed)<?php
$sum = 0;
Arrays::each([1, 2, 3], function ($val) use(&$sum) {
$sum += $val;
});Convert an object into a list of [key, value] pairs.
{array} $array- source array
pairs(array $array): array<?php
$res = Arrays::pairs([
"foo" => "FOO",
"bar" => "BAR",
]);
// [["foo", "FOO"], ["bar", "BAR"]]Test whether all elements in the array pass the test implemented by the provided function.
{array} $array- source array{mixed} $mixed- predicate callback
{mixed} $value- element value{int} $index- zero-based element index{array} $array- source array
every(array $array, mixed $mixed): bool<?php
$res = Arrays::every([1, 2, 3], function($num){ return $num > 1; }); // falseFill all the elements of an array from a start index to an end index with a static value.
{array} $array- source array{mixed} $value- value to fill an array{int} $beginIndex- (optional) start index, defaults to 0{int} $endIndex- (optional) end index, defaults to array length
fill(array $array, $value, int $beginIndex = 0, int $endIndex = null): array<?php
$res = Arrays::fill([1, 2, 3], 4); // [4, 4, 4]
$res = Arrays::fill([1, 2, 3], 4, 1); // [1, 4, 4]Look through each value in the list, returning an array of all the values that pass a truth test (predicate).
{array} $array- source array{callable} $predicate- predicate callback
{mixed} $value- element value{int} $index- zero-based element index{array} $array- source array
filter(array $array, callable $predicate): array<?php
$array = Arrays::filter([1, 2, 3], function($num){ return $num > 1; });Look through each value in the list, returning the first one that passes a truth test (predicate),
{array} $array- source array{callable} $predicate- predicate callback
{mixed} $value- element value{int} $index- zero-based element index{array} $array- source array
find(array $array, callable $predicate)<?php
$value = Arrays::find([1, 2, 3], function($num){ return $num > 1; });Create a new array from an array-like or iterable object
{ArrayObject|\ArrayIterator|Traversable|Object} $collection- source collection
from($collection): array<?php
$res = Arrays::from(new \ArrayObject([1,2,3])); // [1,2,3]
$obj = new \ArrayObject([1,2,3]);
$res = Arrays::from($obj->getIterator()); // [1,2,3]Return a boolean indicating whether the object has the specified property
{array} $array- source array{mixed} $key- the property to test
hasOwnProperty(array $array, mixed $key): bool<?php
$res = Arrays::hasOwnProperty(["foo" => "FOO"], "foo");// trueDetermine whether an array includes a certain element, returning true or false as appropriate.
{array} $array- source array{mixed} $searchElement- the element to search for{int} $fromIndex- (optional) the position in this array at which to begin searching for searchElement.
includes(array $array, $searchElement, int $fromIndex = null): bool<?php
$res = Arrays::includes([1, 2, 3], 2); // true
$res = Arrays::includes([1, 2, 3, 5, 6, 7], 2, 3); // falseReturn the first index at which a given element can be found in the array, or -1 if it is not present
{array} $array- source array{mixed} $searchElement- the element to search for{int} $fromIndex- (optional) the index to start the search at. If the index is greater than or equal to the array's length.
indexOf(array $array, $searchElement, int $fromIndex = 0): int<?php
$src = ["ant", "bison", "camel", "duck", "bison"];
$res = Arrays::indexOf($src, "bison"); // 1
$res = Arrays::indexOf($src, "bison", 2); // 4Determine whether two values are the same value.
{array} $array- source array{array} $arrayToCompare- source array
is(array $array, array $arrayToCompare): bool<?php
$a = [1,2,3];
$b = [1,2,3];
$res = Arrays::is($a, $b); // true
$a = [[["foo" => "FOO", "bar" => "BAR"]]];
$b = [[["foo" => "FOO", "bar" => "BAR"]]];
$res = Arrays::is($a, $b); // trueJoin all elements of an array into a mixed and returns this mixed.
{array} $array- source array{mixed} $separator- (optional) Specifies a mixed to separate each pair of adjacent elements of the array. The separator is converted to a mixed if necessary. If omitted, the array elements are separated with a comma (",").
join(array $array, mixed $separator = ",")<?php
$res = Arrays::join([1,2,3], ":"); // "1:2:3"Return all the keys or a subset of the keys of an array
{array} $array- source array{mixed} $searchValue- (optional) if specified, then only keys containing these values are returned.
keys(array $array, $searchValue = null): array<?php
$res = Arrays::keys(["foo" => "FOO", "bar" => "BAR"]); // ["foo", "bar"]
$res = Arrays::keys(["foo" => "FOO", "bar" => "BAR"], "BAR"); // ["bar"]Return the last index at which a given element can be found in the array, or -1 if it is not present.
{array} $array- source array{mixed} $searchValue- element to locate in the array.{int} $fromIndex- (optional) the index at which to start searching backwards. Defaults to the array's length minus one
lastIndexOf(array $array, $searchElement, int $fromIndex = null): int<?php
$src = [2, 5, 9, 2];
$res = Arrays::lastIndexOf($src, 2); // 3
$res = Arrays::lastIndexOf($src, 2, 2); // 0Produce a new array of values by mapping each value in list through a transformation function
{array} $array- source array{mixed} $mixed- iteratee callback
{mixed} $value- element value{int} $index- zero-based element index{array} $array- source array
map(array $array, mixed $mixed): array<?php
// Map sequential array
$res = Arrays::map([1, 2, 3], function($num){ return $num + 1; });
// Map associative array
$res = Arrays::chain([
"foo" => "FOO",
"bar" => "BAR",
])
->pairs()
->map(function($pair){
// key + value
return $pair[0] . $pair[1];
})
->value();Create a new array with a variable number of arguments, regardless of number or type of the arguments.
{array} ...$args- elements of which to create the array
of(...$args): array<?php
$res = Arrays::of(1, 2, 3); // [1, 2, 3]Remove the last element from an array and returns that element. This method changes the length of the array.
{array} $array- source array
pop(array &$array)<?php
$src = [1, 2, 3];
$res = Arrays::pop($src); // 3Add one or more elements to the end of an array and returns the new length of the array.
{array} $array- source array{mixed} $value- the elements to add to the end of the array
push(array $array, $value): array<?php
$src = [1,2,3];
$res = Arrays::push($src, 4); // [1, 2, 3, 4]The right-associative version of reduce.
{array} $array- source array{mixed} $mixed- iteratee callback{mixed} $initial- value to use as the first argument to the first call of the mixed. If no initial value is supplied, the first element in the array will be used.
{mixed} $value- element value{int} $index- zero-based element index{array} $array- source array
reduceRight(array $array, mixed $mixed, $initial = null)<?php
$res = Arrays::reduceRight([1,2,3], function(array $carry, int $num){
$carry[] = $num;
return $carry;
}, []);
// [3,2,1]Boil down a list of values into a single value.
{array} $array- source array{mixed} $mixed- iteratee callback{mixed} $initial- value to use as the first argument to the first call of the mixed. If no initial value is supplied, the first element in the array will be used.
{mixed} $value- element value{int} $index- zero-based element index{array} $array- source array
reduce(array $array, mixed $mixed, $initial = null)<?php
$sum = Arrays::reduce([1, 2, 3], function ($carry, $num) { return $carry + $num; }, 0);
// 6Reverse an array in place. The first array element becomes the last, and the last array element becomes the first.
{array} $array- source array
reverse(array $array): array<?php
$res = Arrays::reverse([1,2,3]); // [3, 2, 1]Remove the first element from an array and returns that removed element. This method changes the length of the array.
{array} $array- source array
shift(array &$array)<?php
$src = [1, 2, 3];
$res = Arrays::shift($src); // 1Return a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified.
{array} $array- source array{int} $beginIndex- zero-based index at which to begin extraction.{int} $endIndex- (optional) zero-based index before which to end extraction.
slice(array $array, int $beginIndex, int $endIndex = null): array<?php
$src = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
$res = Arrays::slice($src, 1, 3); // ["Orange","Lemon"]Test whether at least one element in the array passes the test implemented by the provided function
{array} $array- source array{mixed} $mixed- predicate callback
{mixed} $value- element value{int} $index- zero-based element index{array} $array- source array
some(array $array, mixed $mixed): bool<?php
$res = Arrays::some([1, 2, 3], function($num){ return $num > 1; }); // trueSort an array by values (using a user-defined comparison function when callback provided)
{array} $array- source array{mixed} $mixed- iteratee callback
sort(array $array, mixed $mixed = null): array<?php
$res = Arrays::sort([3,2,1]); // [1,2,3]
$res = Arrays::sort([3,2,1], function($a, $b){
return $a <=> $b;
}); // [1,2,3]Change the contents of an array by removing existing elements and/or adding new elements.
{array} $array- source array{int} $beginIndex- index at which to start changing the array (with origin 0){int} $deleteCount- (optional) an integer indicating the number of old array elements to remove.{array} ...$items- (optional) the elements to add to the array, beginning at the start index.
splice(array $array, int $beginIndex, int $deleteCount = null, ...$items): array<?php
// remove 1 element from index 2, and insert "trumpet"
$src = ["angel", "clown", "drum", "sturgeon"];
$res = Arrays::splice($src, 2, 1, "trumpet"); // ["angel", "clown", "trumpet", "sturgeon"]Add one or more elements to the beginning of an array and returns the new length of the array
{array} $array- source array{array} ...$values- the elements to add to the front of the array
unshift(array &$array, ...$values)<?php
$src = [1, 2];
$src = Arrays::unshift($src, 0); // [0, 1, 2]
$res = Arrays::unshift($src, -2, -1); // [-2, -1, 0, 1, 2]Return all the values of an array
{array} $array- source array
values(array $array): array<?php
$res = Arrays::values([ 5 => 1, 10 => 2, 100 => 3]); // [1,2,3]Look through each value in the list, returning an array of all the values that contain all of the key-value pairs listed in $conditions
{array} $array- source array{array} $conditions- key-value pairs to match
where(array $array, array $conditions): array<?php
$listOfPlays = [
["title" => "Cymbeline", "author" => "Shakespeare", "year" => 1611],
["title" => "The Tempest", "author" => "Shakespeare", "year" => 1611],
["title" => "Hamlet", "author" => "Shakespeare", "year" => 1603]
];
$res = Arrays::where($listOfPlays, ["author" => "Shakespeare", "year" => 1611]);
// [
// ["title" => "Cymbeline", "author" => "Shakespeare", "year" => 1611],
// ["title" => "The Tempest", "author" => "Shakespeare", "year" => 1611],
// ]Looks through the list and returns the first value that matches all of the key-value pairs listed in properties.
{array} $array- source array{array} $props- key-value pairs to match
findWhere(array $array, array $props)<?php
$res = Arrays::findWhere([ [ "foo" => "FOO", "bar" => "BAR" ], [ "baz" => "BAZ" ] ], [ "foo" => "FOO" ]);
// [ "foo" => "FOO", "bar" => "BAR" ]Return the values in list without the elements that the predicate passes. The opposite of filter.
{array} $array- source array
reject(array $array, mixed $predicate)<?php
$res = Arrays::reject([1, 2, 3, 4, 5, 6], function ($num){
return $num % 2 == 0;
}); // [1,3,5]Returns true if the value is present in the list.
{array} $array- source array{mixed} $searchElement- the element to search for{int} $fromIndex- (optional) the position in this array at which to begin searching for searchElement.
contains(array $array, $searchElement, int $fromIndex = null): boolCalls the method named by methodName on each value in the list. Any extra arguments passed to invoke will be forwarded on to the method invocation.
{array} $array- source array{mixed} $iteratee- callback to run on every array element{array} ...$args- (optional) extra arguments to pass in the callback
invoke(array $array, mixed $iteratee, ...$args): array<?php
$res = Arrays::invoke([[5, 1, 7], [3, 2, 1]], [Arrays::class, "sort"]);
// [ [1, 5, 7], [1, 2, 3] ]A convenient version of what is perhaps the most common use-case for map: extracting a list of property values.
{array} $array- source array{mixed} $key- source property name
pluck(array $array, mixed $key): array<?php
$res = Arrays::pluck([
["name" => "moe", "age" => 40],
["name" => "larry", "age" => 50],
["name" => "curly", "age" => 60],
], "name"); // ["moe, "larry", "curly" ]
Returns the maximum value in list. If an iteratee function is provided
{array} $array- source array{mixed} $iteratee- (optional) callback to compare array element{object} $context- (optional) context to bind to
max(array $array, mixed $iteratee = null, $context = null)<?php
$res = Arrays::max([1,2,3]); // 3
$res = Arrays::max([
["name" => "moe", "age" => 40],
["name" => "larry", "age" => 50],
["name" => "curly", "age" => 60],
], function($stooge){
return $stooge["age"];
});
// ["name" => "curly", "age" => 60]Returns the minimum value in list. If an iteratee function is provided
{array} $array- source array{mixed} $iteratee- (optional) callback to compare array element{object} $context- (optional) context to bind to
min(array $array, mixed $iteratee = null, $context = null)<?php
$res = Arrays::min([1,2,3]); // 1
$res = Arrays::min([
["name" => "moe", "age" => 40],
["name" => "larry", "age" => 50],
["name" => "curly", "age" => 60],
], function($stooge){
return $stooge["age"];
});
// ["name" => "moe", "age" => 40]Return a (stably) sorted copy of list, ranked in ascending order by the results of running each value through iteratee. iteratee may also be the mixed name of the property to sort by
{array} $array- source array{mixed|mixed} $mixed- iteratee callback or property{object} $context- (optional) context to bind to
sortBy(array $array, $iteratee, $context = null): array<?php
$res = Arrays::sortBy([1, 2, 3, 4, 5, 6], function($a){
return \sin($a);
}); // [5, 4, 6, 3, 1, 2]
$res = Arrays::sortBy([
["name" => "moe", "age" => 40],
["name" => "larry", "age" => 50],
["name" => "curly", "age" => 60],
], "name");
// [["name" => "curly", "age" => 60],...]Split a collection into sets, grouped by the result of running each value through iterator
{array} $array- source array{mixed|mixed} $mixed- iteratee callback or property{object} $context- (optional) context to bind to
groupBy(array $array, $iteratee, $context = null): array<?php
$res = Arrays::groupBy([1.3, 2.1, 2.4], function($num) { return floor($num); });
// [1 => [ 1.3 ], 2 => [ 2.1, 2.4 ]]Given a list, and an iteratee function that returns a key for each element in the list (or a property name), returns an object with an index of each item. Just like groupBy, but for when you know your keys are unique.
{array} $array- source array{mixed|mixed} $mixed- iteratee callback or property{object} $context- (optional) context to bind to
indexBy(array $array, $iteratee, $context = null): array<?php
$res = Arrays::indexBy([
["name" => "moe", "age" => 40],
["name" => "larry", "age" => 50],
["name" => "curly", "age" => 60],
], "name");
// [ 40 => ["name" => "moe", "age" => 40], ...]Sort a list into groups and return a count for the number of objects in each group.
{array} $array- source array{mixed|mixed} $mixed- iteratee callback or property{object} $context- (optional) context to bind to
countBy(array $array, $iteratee, $context = null): array<?php
$res = Arrays::countBy([1, 2, 3, 4, 5], function($num) {
return $num % 2 == 0 ? "even": "odd";
});
// [ "odd => 3, "even" => 2 ]Return a shuffled copy of the list
{array} $array- source array
shuffle(array $array): array<?php
$res = Arrays::shuffle([1, 2, 3]); // [ 2, 1, 3 ]Produce a random sample from the list. Pass a number to return n random elements from the list. Otherwise a single random item will be returned.
{array} $array- source array{int} $count- (optional) number of random elements
sample(array $array, int $count = null)<?php
$res = Arrays::sample([1, 2, 3], 3); // [ 2, 1, 3 ]Creates a real Array from the list
{ArrayObject|\ArrayIterator|Traversable|Object} $collection- source collection
toArray($collection): arrayReturn the number of values in the list.
{array} $array- source array
size(array $array): int<?php
$res = Arrays::size([
"one" => 1,
"two" => 2,
"three" => 3
]); // 3split array into two arrays: one whose elements all satisfy predicate and one whose elements all do not satisfy predicate.
{array} $array- source arraymixed|mixed|Closure $mixed- predicate callback
partition(array $array, mixed $mixed): array<?php
$res = Arrays::partition([0, 1, 2, 3, 4, 5], function($val) {
return $val % 2;
}); // [[1, 3, 5], [0, 2, 4]]Get the first value from an array regardless index order and without modifying the array
When passed in array has no element, it returns undefined, unless $defaultValue is supplied.
Then it returns $defaultValue (when $defaultValue is a mixed then the result of its execution)
{array} $array- source array{mixed} $defaultValue- (optional) scalar or mixed
first(array $array, $defaultValue = null): mixed<?php
$element = Arrays::first([1, 2, 3]);
$element = Arrays::first($arr, 1);
$element = Arrays::first($arr, function(){ return 1; });Returns everything but the last entry of the array. Especially useful on the arguments object. Pass count to exclude the last count elements from the result.
{array} $array- source array{int} $count- (optional) number of elements to remove
initial(array $array, int $count = 1): array<?php
$res = Arrays::initial([5, 4, 3, 2, 1]); // [5, 4, 3, 2]
//...
$res = Arrays::initial([5, 4, 3, 2, 1], 3); // [5, 4]Get the last value from an array regardless index order and without modifying the array
{array} $array- source array
last(array $array)<?php
$element = Arrays::last([1, 2, 3]);Returns the rest of the elements in an array. Pass an index to return the values of the array from that index onward.
{array} $array- source array{int} $count- (optional) number of elements to remove
rest(array $array, int $count = 1): array<?php
$res = Arrays::rest([5, 4, 3, 2, 1]); // [4, 3, 2, 1]
//...
$res = Arrays::rest([5, 4, 3, 2, 1], 3); // [2, 1]Returns a copy of the array with all falsy values removed.
{array} $array- source array
compact(array $array): array<?php
$res = Arrays::compact([0, 1, false, 2, '', 3]); // [1, 2, 3]Flattens a nested array (the nesting can be to any depth). If you pass shallow, the array will only be flattened a single level.
{array} $array- source array{bool} $shallow- (optional) when true single level of flattering
flatten(array $array, bool $shallow = false): array<?php
$res = Arrays::flatten([1, [2], [3, [[4]]]]); // [1, 2, 3, 4]
//...
$res = Arrays::flatten([1, [2], [3, [[4]]]], true); // [1, 2, 3, [[4]]]Returns a copy of the array with all instances of the values removed.
{array} $array- source array{array} $values- values to remove
without(array $array, ...$values): array<?php
$res = Arrays::without([1, 2, 1, 0, 3, 1, 4], 0, 1); // [2, 3, 4]Computes the union of the passed-in arrays: the list of unique items, in order, that are present in one or more of the arrays.
{array} ...$args- arrays to join
union(...$args): array<?php
$res = Arrays::union(
[1, 2, 3],
[101, 2, 1, 10],
[2, 1]
); // [1, 2, 3, 101, 10]Computes the list of values that are the intersection of all the arrays. Each value in the result is present in each of the arrays.
{array} $array- source array{array} ...$sources- source arrays
intersection(array $array, ...$sources): array<?php
$res = Arrays::intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]); // [1, 2]
$res = Arrays::intersection(
["a" => "green", "b" => "brown", "c" => "blue", "red"],
["a" => "green", "b" => "yellow", "blue", "red"]
); // [ "a" => "green" ]Returns the values from array that are not present in the other arrays.
{array} $array- source array{array} ...$sources- source arrays
difference(array $array, ...$sources): array<?php
$res = Arrays::difference([ 1, 2, 3, 4, 5], [5, 2, 10]); // [1, 3, 4]
$res = Arrays::difference(
["a" => "green", "b" => "brown", "c" => "blue", "red"],
["a" => "green", "yellow", "red"]
);
// [ "b" => "brown", "c" => "blue", "red" ]Produces a duplicate-free version of the array
{array} $array- source array
uniq(array $array): array<?php
$res = Arrays::uniq([1,2,3,1,1,2]); // [1,2,3]Merges together the values of each of the arrays with the values at the corresponding position. Useful when you have separate data sources that are coordinated through matching array indexes.
{array} $array- source array...$sources- source arrays
zip(array $array, ...$sources): array<?php
$res = Arrays::zip(
["moe", "larry", "curly"],
[30, 40, 50],
[true, false, false]
); // [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]The opposite of zip. Given an array of arrays, returns a series of new arrays, the first of which contains all of the first elements in the input arrays, the second of which contains all of the second elements, and so on.
{array} $array- source array
unzip(array $array): array<?php
$res = Arrays::unzip([["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]);
// [["moe", "larry", "curly"], [30, 40, 50], [true, false, false]]Converts arrays into objects. Pass either a single list of [key, value] pairs, or a list of keys, and a list of values. If duplicate keys exist, the last value wins.
{array} $array- source array{array} $value- (optional) values array
object(array $array, array $values = null): PlainObject$obj = Arrays::object([ "foo" =>
[
"bar" => [
"baz" => "BAZ"
]
]
]);
echo $obj->foo->bar->baz; // BAZ$obj = Arrays::object([["moe", 30], ["larry", 40], ["curly", 50]]);
echo $obj->moe; // 30
echo $obj->larry; // 40
echo $obj->curly; // 50$obj = Arrays::object(["moe", "larry", "curly"], [30, 40, 50]);
echo $obj->moe; // 30
echo $obj->larry; // 40
echo $obj->curly; // 50Uses a binary search to determine the index at which the value should be inserted into the list in order to maintain the list's sorted order. If an iteratee function is provided, it will be used to compute the sort ranking of each value, including the value you pass. The iteratee may also be the mixed name of the property to sort by
{array} $array- source array{mixed} $value- value to insert{mixed|mixed} $iteratee- (optional) iteratee callback{object} $context- (optional) context to bind to
sortedIndex(array $array, $value, $iteratee = null, $context = null): int<?php
$res = Arrays::sortedIndex([10, 20, 30, 40, 50], 35); // 3<?php
$stooges = [
["name" => "moe", "age" => 40],
["name" => "larry", "age" => 50],
["name" => "curly", "age" => 60],
];
$res = Arrays::sortedIndex($stooges, ["name" => "larry", "age" => 50], "age"); // 1Find index of the first element matching the condition in $mixed
{array} $array- source array{mixed|mixed} $iteratee- (optional) iteratee callback{object} $context- (optional) context to bind to
findIndex(array $array, $iteratee = null, $context = null): int<?php
$inx = Arrays::findIndex([
["val" => "FOO"],
["val" => "BAR"],
], function ($item){
return $item["val"] === "BAR";
}); // 1Like findIndex but iterates the array in reverse, returning the index closest to the end where the predicate truth test passes.
{array} $array- source array{mixed|mixed} $iteratee- (optional) iteratee callback{object} $context- (optional) context to bind to
findLastIndex(array $array, $iteratee = null, $context = null): int<?php
$src = [
[
'id' => 1,
'name' => 'Ted',
'last' => 'White',
],
[
'id' => 2,
'name' => 'Bob',
'last' => 'Brown',
],
[
'id' => 3,
'name' => 'Ted',
'last' => 'Jones',
],
];
$res = Arrays::findLastIndex($src, [ "name" => "Ted" ]); // 2A function to create flexibly-numbered lists of integers, handy for each and map loops. start, if omitted, defaults to 0; step defaults to 1. Returns a list of integers from start (inclusive) to stop (exclusive), incremented (or decremented) by step, exclusive. Note that ranges that stop before they start are considered to be zero-length instead of negative — if you'd like a negative range, use a negative step.
{int} $start- start value{int} $end- (optional) end value{int} $step- (optional) step value
range(int $start, int $end = null, int $step = 1): array<?php
$res = Arrays::range(10); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<?php
$res = Arrays::range(1, 11); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<?php
$res = Arrays::range(0, 30, 5); // [0, 5, 10, 15, 20, 25]<?php
$res = Arrays::range(0, -10, -1);
// [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]Returns a wrapped object. Calling methods on this object will continue to return wrapped objects until value is called.
{array} $array- source array
chain($array): Arrays<?php
$res = Arrays::chain([1, 2, 3])
->map(function($num){ return $num + 1; })
->filter(function($num){ return $num > 1; })
->reduce(function($carry, $num){ return $carry + $num; }, 0)
->value();Retrieve all the names of object's own and inherited properties. Given we consider here JavaScript object as associative array, it is an alias of [keys](#keys]
{array} $array- source array
allKeys(array $array): arrayLike map, but for associative arrays. Transform the value of each property in turn.
{array} $array- source array{callable} $iteratee- iteratee function{object} $context- (optional) context object to bind to
mapObject(array $array, callable $iteratee, $context = null): array<?php
$res = Arrays::mapObject([
"start" => 5,
"end" => 12,
], function($val){
return $val + 5;
}); // [ "start" => 10, "end" => 17, ]Convert an object into a list of [key, value] pairs. Alias of entries
{array} $array- source array
pairs(array $array): array<?php
$res = Arrays::pairs(["foo" => "FOO", "bar" => "BAR"]); // [["foo", "FOO"], ["bar", "BAR"]]Returns a copy of the object where the keys have become the values and the values the keys. For this to work, all of your object's values should be unique and string serializable.
{array} $array- source array
invert(array $array): array<?php
$res = Arrays::invert([
"Moe" => "Moses",
"Larry" => "Louis",
"Curly" => "Jerome",
]);
// [
// "Moses" => "Moe",
// "Louis" => "Larry",
// "Jerome" => "Curly",
// ]Similar to findIndex, but for keys in objects. Returns the key where the predicate truth test passes or undefined
{array} $array- source array{callable} $iteratee- iteratee function{object} $context- (optional) context object to bind to
findKey(array $array, $iteratee = null, $context = null): string<?php
$src = [
"foo" => [
'name' => 'Ted',
'last' => 'White',
],
"bar" => [
'name' => 'Frank',
'last' => 'James',
],
"baz" => [
'name' => 'Ted',
'last' => 'Jones',
],
];
$res = Arrays::findKey($src, [ "name" => "Ted" ]); // fooShallowly copy all of the properties in the source objects over to the destination object, and return the destination object. Any nested objects or arrays will be copied by reference Given we consider here JavaScript object as associative array, it's an alias of assign
{array} ...$arrays- arrays to merge
extend(... $arrays): array<?php
$res = Arrays::extend(["foo" => 1, "bar" => 2], ["bar" => 3], ["foo" => 4], ["baz" => 5]);
// ["foo" => 4, "bar" => 3, "baz" => 5]Return a copy of the object, filtered to only have values for the whitelisted keys (or array of valid keys). Alternatively accepts a predicate indicating which keys to pick.
{array} $array- source array{array} ...$keys- keys or iteratee function
pick(array $array, ...$keys): array<?php
$res = Arrays::pick([
'name' => 'moe',
'age' => 50,
'userid' => 'moe1',
], 'name', 'age');
// ['name' => 'moe', 'age' => 50, ]<?php
$res = Arrays::pick([
'name' => 'moe',
'age' => 50,
'userid' => 'moe1',
], function($value){
return is_int($value);
});
// ['age' => 50 ]Return a copy of the object, filtered to omit the blacklisted keys (or array of keys). Alternatively accepts a predicate indicating which keys to omit.
{array} $array- source array{array} ...$keys- keys or iteratee function
omit(array $array, ...$keys): array<?php
$res = Arrays::omit([
'name' => 'moe',
'age' => 50,
'userid' => 'moe1',
], 'userid');
// ['name' => 'moe', 'age' => 50, ]<?php
$res = Arrays::omit([
'name' => 'moe',
'age' => 50,
'userid' => 'moe1',
], function($value){
return is_int($value);
});
// ['name' => 'moe', 'userid' => 'moe1', ]Fill in undefined properties in object with the first value present in the following list of defaults objects.
{array} $array- source array{array} $defaults- key-value array of defaults
defaults(array $array, array $defaults): array<?php
$res = Arrays::defaults([
"flavor" => "chocolate"
], [
"flavor" => "vanilla",
"sprinkles" => "lots",
]); //["flavor" => "chocolate", "sprinkles" => "lots", ]Does the object contain the given key? Identical to object.hasOwnProperty(key), but uses a safe reference to the hasOwnProperty function, in case it's been overridden accidentally.
{array} $array- source array{string} $key- key to look for
has(array $array, string $key): bool<?php
$res = Arrays::has([
"flavor" => "vanilla",
"sprinkles" => "lots",
], "flavor"); // trueReturns a function that will itself return the key property of any passed-in object.
{string} $prop- property name
property(string $prop): callable<?php
$stooge = [ "name" => "moe" ];
$res = Arrays::property("name")($stooge); // "moe"Inverse of property. Takes an object and returns a function which will return the value of a provided property.
{array} $array- source key-value array
propertyOf(array $array): callable<?php
$stooge = [ "name" => "moe" ];
$res = Arrays::propertyOf($stooge)("name"); // "moe"Returns a predicate function that will tell you if a passed in object contains all of the key/value properties present in attrs.
{array} $attrs- attributes to check
matcher(array $attrs): callable<?php
$src = [
[
"foo" => "FOO",
"bar" => "BAR",
],
[
"bar" => "BAR",
"baz" => "BAZ",
]
];
$matcher = Arrays::matcher([
"foo" => "FOO",
"bar" => "BAR",
]);
$res = Arrays::filter($src, $matcher);
// [[ "foo" => "FOO", "bar" => "BAR", ]]Returns true if an enumerable object contains no values (no enumerable own-properties).
{array} $array- source array
isEmpty(array $array): bool<?php
$res = Arrays::isEmpty([]); // truePerforms an optimized deep comparison between the two objects, to determine if they should be considered equal.
{array} $array- source array{array} $target- array to compare with
isEqual(array $array, array $target): bool<?php
$res = Arrays::isEqual([
"name" => "moe",
"luckyNumbers" => [13, 27, 34],
], [
"name" => "moe",
"luckyNumbers" => [13, 27, 34],
]); // trueTells you if the keys and values in properties are contained in object.
{array} $array- source key-value array{array} $attrs- attributes to check
isMatch(array $array, array $attrs): bool<?php
$res = Arrays::isMatch([
"foo" => "FOO",
"bar" => "BAR",
"baz" => "BAZ",
],
[
"foo" => "BAZ",
]); // falseReturns true if source is an array.
{array} $array- source array
isArray(array $array): bool<?php
$res = Arrays::isArray([ 1, 2, 3 ]); // trueReturns true if value is an Object. Given we consider here JavaScript object as associative array, it is an alias of isAssocArray
{mixed} $source- source
isObject($source): bool<?php
$res = Arrays::isObject([ "foo" => 1, "bar" => 1, ]); // trueReplace (similar to MYSQL REPLACE statement) an element matching the condition in predicate function with the value If no match found, add the value to the end of array
{array} $array- source array{mixed} $mixed- predicate callback{mixed} $element- element to replace the match
replace(array $array, mixed $mixed, $element): array<?php
$array = Arrays::replace([
["val" => "FOO"],
["val" => "BAR"],
], function ($item){
return $item["val"] === "BAR";
}, ["val" => "BAZ"]);Test whether array is not sequential, but associative array
{array} $array- source array
isAssocArray(array $array): bool<?php
$bool = Arrays::isAssocArray([1, 2, 3]); // false