Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
L

lodash-php

> 编程语言
Open source

Easy to use utility functions for everyday PHP projects. This is a port of the Lodash JS library to PHP

523 stars0 likes3 views
WebsiteGitHub

About

Easy to use utility functions for everyday PHP projects. This is a port of the Lodash JS library to PHP

Lodash-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

Requirements

Lodash-PHP requires minimum PHP 7.2+, but the latest version of PHP is always recommended.

Installation

Install Lodash-PHP through composer:

bash
$ composer require lodash-php/lodash-php

Usage

Each method in Lodash-PHP is a separate function that can be imported and used on it's own.

php
 [['a', 'b'], ['c', 'd']]

chunk(['a', 'b', 'c', 'd'], 3)
// => [['a', 'b', 'c'], ['d']]

compact

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:

php
 [1, 2, 3]

concat

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:

php
 [1, 2, 3, [4]]

var_dump($array)
// => [1]

difference

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:

php
 [1]

differenceBy

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:

php
 [1.2]

differenceWith

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:

php
 1, 'y' => 2 ], [ 'x' => 2, 'y' => 1 ]]

differenceWith($objects, [[ 'x' => 1, 'y' => 2 ]], '_::isEqual')
// => [[ 'x' => 2, 'y' => 1 ]]

drop

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:

php
 [2, 3]

drop([1, 2, 3], 2)
// => [3]

drop([1, 2, 3], 5)
// => []

drop([1, 2, 3], 0)
// => [1, 2, 3]

dropRight

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:

php
 [1, 2]

dropRight([1, 2, 3], 2)
// => [1]

dropRight([1, 2, 3], 5)
// => []

dropRight([1, 2, 3], 0)
// => [1, 2, 3]

dropRightWhile

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:

php
 'barney',  'active' => false ],
[ 'user' => 'fred',    'active' => true ],
[ 'user' => 'pebbles', 'active' => true ]
]

dropRightWhile($users, function($user) { return $user['active']; })
// => objects for ['barney']

dropWhile

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:

php
 'barney',  'active' => true ],
[ 'user' => 'fred',    'active' => true ],
[ 'user' => 'pebbles', 'active' => false ]
]

dropWhile($users, function($user) { return $user['active']; } )
// => objects for ['pebbles']

every

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:

php
 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

findIndex

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:

php
 '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

findLastIndex

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:

php
 'barney',  'active' => true ],
['user' => 'fred',    'active' => false ],
['user' => 'pebbles', 'active' => false ]
]

findLastIndex($users, function($user) { return $user['user'] === 'pebbles'; })
// => 2

flatten

Flattens array a single level deep.

Arguments:

@param array $array The array to flatten.

Return:

@return array the new flattened array.

Example:

php
 [1, 2, [3, [4]], 5]

flattenDeep

Recursively flattens array.

Arguments:

@param array $array The array to flatten.

Return:

@return array Returns the new flattened array.

Example:

php
 [1, 2, 3, 4, 5]

flattenDepth

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:

php
 [1, 2, [3, [4]], 5]

flattenDepth($array, 2)
// => [1, 2, 3, [4], 5]

fromPairs

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:

php
 stdClass(
// 'a' => 1,
//'b' => 2,
// )

head

Gets the first element of array.

Arguments:

@param array $array The array to query.

Return:

@return mixed Returns the first element of array.

Example:

php
 1

head([])
// => null

indexOf

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:

php
 1

// Search from the `fromIndex`.
indexOf([1, 2, 1, 2], 2, 2)
// => 3

initial

Gets all but the last element of array.

Arguments:

@param array $array The array to query.

Return:

@return array the slice of array.

Example:

php
 [1, 2]

intersection

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:

php
 [2]

intersectionBy

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:

php
 [2.1]

// The `property` iteratee shorthand.
intersectionBy([[ 'x' => 1 ]], [[ 'x' => 2 ], [ 'x' => 1 ]], 'x');
// => [[ 'x' => 1 ]]

intersectionWith

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:

php
 1, 'y' => 2 ], [ 'x' => 2, 'y' => 1 ]]
$others = [[ 'x' => 1, 'y' => 1 ], [ 'x' => 1, 'y' => 2 ]]

intersectionWith($objects, $others, '_::isEqual')
// => [[ 'x' => 1, 'y' => 2 ]]

last

Gets the last element of array.

Arguments:

@param array $array The array to query.

Return:

@return mixed Returns the last element of array.

Example:

php
 3

lastIndexOf

This method is like indexOf except that it iterates over elements of array from right to left.

Arguments:

@param array $a

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPlodashphpphp-libraryphp-utility

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言