适用于日常 PHP 项目的易于使用的实用函数。这是将 Lodash JS 库移植到 PHP 的结果。
Lodash-PHP is a port of the Lodash JS library to PHP. It is a set of easy to use utility functions for everyday PHP projects.
Lodash-PHP tries to mimick lodash.js as close as possible
Lodash-PHP requires minimum PHP 7.2+, but the latest version of PHP is always recommended.
Install Lodash-PHP through composer:
$ composer require lodash-php/lodash-phpEach method in Lodash-PHP is a separate function that can be imported and used on it's own.
[['a', 'b'], ['c', 'd']]
chunk(['a', 'b', 'c', 'd'], 3)
// => [['a', 'b', 'c'], ['d']]
Creates an array with all falsey values removed. The values false, null,
0, "", undefined, and NaN are falsey.
Arguments:
@param array $array The array to compact.
Return:
@return array Returns the new array of filtered values.
Example:
[1, 2, 3]
Creates a new array concatenating array with any additional arrays
and/or values.
Arguments:
@param array $array The array to concatenate.
@param array $values The values to concatenate.
Return:
@return array Returns the new concatenated array.
Example:
[1, 2, 3, [4]]
var_dump($array)
// => [1]
Creates an array of array values not included in the other given arrays
using SameValueZero
for equality comparisons. The order and references of result values are
determined by the first array.
Note: Unlike pullAll, this method returns a new array.
Arguments:
@param array $array The array to inspect.
@param array ...$values The values to exclude.
Return:
@return array Returns the new array of filtered values.
Example:
[1]
This method is like difference except that it accepts iteratee which
is invoked for each element of array and values to generate the criterion
by which they're compared. The order and references of result values are
determined by the first array. The iteratee is invoked with one argument:
(value).
Note: Unlike pullAllBy, this method returns a new array.
Arguments:
@param array $array The array to inspect.
@param array ...$values The values to exclude.
@param callable $iteratee The iteratee invoked per element.
Return:
@return array Returns the new array of filtered values.
Example:
[1.2]
This method is like difference except that it accepts comparator
which is invoked to compare elements of array to values. The order and
references of result values are determined by the first array. The comparator
is invoked with two arguments: (arrVal, othVal).
Note: Unlike pullAllWith, this method returns a new array.
Arguments:
@param array $array The array to inspect.
@param array ...$values The values to exclude.
@param callable $comparator The comparator invoked per element.
Return:
@return array Returns the new array of filtered values.
Example:
1, 'y' => 2 ], [ 'x' => 2, 'y' => 1 ]]
differenceWith($objects, [[ 'x' => 1, 'y' => 2 ]], '_::isEqual')
// => [[ 'x' => 2, 'y' => 1 ]]
Creates a slice of array with n elements dropped from the beginning.
NOTE: This function will reorder and reset the array indices
Arguments:
@param array $array The array to query.
@param int $n The number of elements to drop.
Return:
@return array the slice of array.
Example:
[2, 3]
drop([1, 2, 3], 2)
// => [3]
drop([1, 2, 3], 5)
// => []
drop([1, 2, 3], 0)
// => [1, 2, 3]
Creates a slice of array with n elements dropped from the end.
NOTE: This function will reorder and reset the array indices
Arguments:
@param array $array The array to query.
@param int $n The number of elements to drop.
Return:
@return array the slice of array.
Example:
[1, 2]
dropRight([1, 2, 3], 2)
// => [1]
dropRight([1, 2, 3], 5)
// => []
dropRight([1, 2, 3], 0)
// => [1, 2, 3]
Creates a slice of array excluding elements dropped from the end.
Elements are dropped until predicate returns falsey. The predicate is
invoked with three arguments: (value, index, array).
Arguments:
@param array $array The array to query.
@param callable $predicate The function invoked per iteration.
Return:
@return array the slice of array.
Example:
'barney', 'active' => false ],
[ 'user' => 'fred', 'active' => true ],
[ 'user' => 'pebbles', 'active' => true ]
]
dropRightWhile($users, function($user) { return $user['active']; })
// => objects for ['barney']
Creates a slice of array excluding elements dropped from the beginning.
Elements are dropped until predicate returns falsey. The predicate is
invoked with three arguments: (value, index, array).
Arguments:
@param array $array The array to query.
@param callable $predicate The function invoked per iteration.
Return:
@return array the slice of array.
Example:
'barney', 'active' => true ],
[ 'user' => 'fred', 'active' => true ],
[ 'user' => 'pebbles', 'active' => false ]
]
dropWhile($users, function($user) { return $user['active']; } )
// => objects for ['pebbles']
Checks if predicate returns truthy for all elements of array.
Iteration is stopped once predicate returns falsey. The predicate is
invoked with three arguments: (value, index, array).
Note: This method returns true for
empty arrays because
everything is true of
elements of empty arrays.
Arguments:
@param iterable $collection The array to iterate over.
@param callable $predicate The function invoked per iteration.
Return:
@return bool true if all elements pass the predicate check, else false.
Example:
false
$users = [
['user' => 'barney', 'age' => 36, 'active' => false],
['user' => 'fred', 'age' => 40, 'active' => false],
];
// The `matches` iteratee shorthand.
$this->assertFalse(every($users, ['user' => 'barney', 'active' => false]));
// false
// The `matchesProperty` iteratee shorthand.
$this->assertTrue(every($users, ['active', false]));
// true
// The `property` iteratee shorthand.
$this->assertFalse(every($users, 'active'));
//false
This method is like find except that it returns the index of the first element predicate returns truthy for instead of the element itself.
Arguments:
@param array $array The array to inspect.
@param callable $predicate The function invoked per iteration.
@param int $fromIndex The index to search from.
Return:
@return int the index of the found element, else -1.
Example:
'barney', 'active' => false],
['user' => 'fred', 'active' => false],
['user' => 'pebbles', 'active' => true],
];
findIndex($users, function($o) { return $o['user'] s== 'barney'; });
// => 0
// The `matches` iteratee shorthand.
findIndex($users, ['user' => 'fred', 'active' => false]);
// => 1
// The `matchesProperty` iteratee shorthand.
findIndex($users, ['active', false]);
// => 0
// The `property` iteratee shorthand.
findIndex($users, 'active');
// => 2
This method is like findIndex except that it iterates over elements
of collection from right to left.
Arguments:
@param array $array The array to inspect.
@param mixed $predicate The function invoked per iteration.
@param int $fromIndex The index to search from.
Return:
@return int the index of the found element, else -1.
Example:
'barney', 'active' => true ],
['user' => 'fred', 'active' => false ],
['user' => 'pebbles', 'active' => false ]
]
findLastIndex($users, function($user) { return $user['user'] === 'pebbles'; })
// => 2
Flattens array a single level deep.
Arguments:
@param array $array The array to flatten.
Return:
@return array the new flattened array.
Example:
[1, 2, [3, [4]], 5]
Recursively flattens array.
Arguments:
@param array $array The array to flatten.
Return:
@return array Returns the new flattened array.
Example:
[1, 2, 3, 4, 5]
Recursively flatten array up to depth times.
Arguments:
@param array $array The array to flatten.
@param int $depth The maximum recursion depth.
Return:
@return array the new flattened array.
Example:
[1, 2, [3, [4]], 5]
flattenDepth($array, 2)
// => [1, 2, 3, [4], 5]
The inverse of toPairs, this method returns an object composed
from key-value pairs.
Arguments:
@param array $pairs The key-value pairs.
Return:
@return \stdClass the new object.
Example:
stdClass(
// 'a' => 1,
//'b' => 2,
// )
Gets the first element of array.
Arguments:
@param array $array The array to query.
Return:
@return mixed Returns the first element of array.
Example:
1
head([])
// => null
Gets the index at which the first occurrence of value is found in array
using SameValueZero
for equality comparisons. If fromIndex is negative, it's used as the
offset from the end of array.
Arguments:
@param array $array The array to inspect.
@param mixed $value The value to search for.
@param int $fromIndex The index to search from.
Return:
@return int the index of the matched value, else -1.
Example:
1
// Search from the `fromIndex`.
indexOf([1, 2, 1, 2], 2, 2)
// => 3
Gets all but the last element of array.
Arguments:
@param array $array The array to query.
Return:
@return array the slice of array.
Example:
[1, 2]
Creates an array of unique values that are included in all given arrays
using SameValueZero
for equality comparisons. The order and references of result values are
determined by the first array.
Arguments:
@param array ...$arrays
Return:
@return array the new array of intersecting values.
Example:
[2]
This method is like intersection except that it accepts iteratee
which is invoked for each element of each arrays to generate the criterion
by which they're compared. The order and references of result values are
determined by the first array. The iteratee is invoked with one argument:
(value).
Arguments:
@param array ...$arrays
@param callable $iteratee The iteratee invoked per element.
Return:
@return array the new array of intersecting values.
Example:
[2.1]
// The `property` iteratee shorthand.
intersectionBy([[ 'x' => 1 ]], [[ 'x' => 2 ], [ 'x' => 1 ]], 'x');
// => [[ 'x' => 1 ]]
This method is like intersection except that it accepts comparator
which is invoked to compare elements of arrays. The order and references
of result values are determined by the first array. The comparator is
invoked with two arguments: (arrVal, othVal).
Arguments:
@param array ...$arrays
@param callable $comparator The comparator invoked per element.
Return:
@return array the new array of intersecting values.
Example:
1, 'y' => 2 ], [ 'x' => 2, 'y' => 1 ]]
$others = [[ 'x' => 1, 'y' => 1 ], [ 'x' => 1, 'y' => 2 ]]
intersectionWith($objects, $others, '_::isEqual')
// => [[ 'x' => 1, 'y' => 2 ]]
Gets the last element of array.
Arguments:
@param array $array The array to query.
Return:
@return mixed Returns the last element of array.
Example:
3
This method is like indexOf except that it iterates over elements of
array from right to left.
Arguments:
@param array $a
暂无开放 Issues,或尚未同步最近议题。