lodash-php
Lodash-PHP是php的Lodash JS库的港口。这是一组易于使用的实用程序功能,用于日常PHP项目。
Lodash-PHP尝试尽可能接近Mimick Lodash.js
要求
Lodash-PHP需要最低php 7.2+,但始终建议使用最新版本的PHP。
安装
通过作曲家安装Lodash-PHP:
$ composer require lodash-php/lodash-php用法
Lodash-PHP中的每种方法都是一个单独的功能,可以单独导入和使用。
use function _ each ;
each ([ 1 , 2 , 3 ], function ( int $ item ) {
var_dump ( $ item );
}); Lodash-PHP还带有一个全球使用的全局_类。
_:: each ([ 1 , 2 , 3 ], function ( int $ item ) {
var_dump ( $ item );
});方法
- 大批
- 收藏
- 日期
- 功能
- 朗
- 数学
- 数字
- 目的
- seq
- 细绳
- util
大批
大块
创建一系列元素分为size的长度。如果array不能均匀分开,则最终的块将是其余元素。
参数:
@Param数组$阵列数组阵列要处理。
@param int $ [size = 1]每个块的长度
返回:
@return数组返回新的块。
例子:
use function _ chunk ;
chunk ([ ' a ' , ' b ' , ' c ' , ' d ' ], 2 )
// => [['a', 'b'], ['c', 'd']]
chunk ([ ' a ' , ' b ' , ' c ' , ' d ' ], 3 )
// => [['a', 'b', 'c'], ['d']]袖珍的
创建一个删除所有假值的数组。值false , null , 0 , "" , undefined ”和NaN是错误的。
参数:
@param数组$阵列compact。
返回:
@return数组返回过滤值的新数组。
例子:
use function _ compact ;
compact ([ 0 , 1 , false , 2 , '' , 3 ])
// => [1, 2, 3]concat
与任何其他数组和/或值创建一个新的数组串联array 。
参数:
@param数组$阵列将数组与连接酸盐。
@param数组
返回:
@return数组返回新的串联阵列。
例子:
use function _ concat ;
$ array = [ 1 ];
$ other = concat ( $ array , 2 , [ 3 ], [[ 4 ]]);
var_dump ( $ other )
// => [1, 2, 3, [4]]
var_dump ( $ array )
// => [1]不同之处
创建使用SameValueZero中的其他给定数组中未包含的array值数组,以进行平等比较。结果值的顺序和参考由第一个数组确定。
注意:与pullAll不同,此方法返回一个新数组。
参数:
@param数组$阵列要检查的数组。
@param数组... $值排除的值。
返回:
@return数组返回过滤值的新数组。
例子:
use function _ difference ;
difference ([ 2 , 1 ], [ 2 , 3 ])
// => [1]差异
此方法就像difference一样,除了它接受iteratee ,该iTerateE是为array的每个元素和values所调用的,以生成比较它们的标准。结果值的顺序和参考由第一个数组确定。 ITEMERE被一个参数调用:(value)。
注意:与pullAllBy不同,此方法返回一个新数组。
参数:
@param数组$阵列要检查的数组。
@param数组
@param callable $ iteratee the iteratee每个元素调用。
返回:
@return数组返回过滤值的新数组。
例子:
use function _ differenceBy ;
differenceBy ([ 2.1 , 1.2 ], [ 2.3 , 3.4 ], ' floor ' )
// => [1.2]差异
此方法就像difference一样,除了它接受comparator ,该比较器被调用以将array的元素与values进行比较。结果值的顺序和参考由第一个数组确定。比较器用两个参数调用:( Arrval,othval)。
注意:与pullAllWith不同,此方法返回一个新数组。
参数:
@param数组
@param数组... $值排除的值。
@param callable $比较器每个元素调用比较器。
返回:
@return数组返回过滤值的新数组。
例子:
use function _ differenceWith ;
$ objects = [[ ' x ' => 1 , ' y ' => 2 ], [ ' x ' => 2 , ' y ' => 1 ]]
differenceWith ( $ objects , [[ ' x ' => 1 , ' y ' => 2 ]], ' _::isEqual ' )
// => [[ 'x' => 2, 'y' => 1 ]]降低
从一开始就可以创建一个array ,其中n元素掉落。
注意:此功能将重新排序并重置数组索引
参数:
@param数组$阵列将数组到查询。
@param int $ n要删除的元素数量。
返回:
@Return数组array的切片。
例子:
use function _ drop ;
drop ([ 1 , 2 , 3 ])
// => [2, 3]
drop ([ 1 , 2 , 3 ], 2 )
// => [3]
drop ([ 1 , 2 , 3 ], 5 )
// => []
drop ([ 1 , 2 , 3 ], 0 )
// => [1, 2, 3]滴定
创建一片array ,从末端掉落n元素。注意:此功能将重新排序并重置数组索引
参数:
@param数组$阵列将数组到查询。
@param int $ n要删除的元素数量。
返回:
@Return数组array的切片。
例子:
use function _ dropRight ;
dropRight ([ 1 , 2 , 3 ])
// => [1, 2]
dropRight ([ 1 , 2 , 3 ], 2 )
// => [1]
dropRight ([ 1 , 2 , 3 ], 5 )
// => []
dropRight ([ 1 , 2 , 3 ], 0 )
// => [1, 2, 3]掉线时
创建一片array ,不包括从末端掉落的元素。删除元素,直到predicate返回虚假。谓词带有三个参数:(值,索引,数组)。
参数:
@param数组$阵列将数组到查询。
@param callable $ pashitate the tecoke the tecoke the tecoke the迭代调用。
返回:
@Return数组array的切片。
例子:
use function _ dropRightWhile ;
$ users = [
[ ' user ' => ' barney ' , ' active ' => false ],
[ ' user ' => ' fred ' , ' active ' => true ],
[ ' user ' => ' pebbles ' , ' active ' => true ]
]
dropRightWhile ( $ users , function ( $ user ) { return $ user [ ' active ' ]; })
// => objects for ['barney']下降
创建一片array ,不包括从一开始就掉落的元素。删除元素,直到predicate返回虚假。谓词带有三个参数:(值,索引,数组)。
参数:
@param数组$阵列将数组到查询。
@param callable $ pashitate the tecoke the tecoke the tecoke the迭代调用。
返回:
@Return数组array的切片。
例子:
use function _ dropWhile ;
$ users = [
[ ' user ' => ' barney ' , ' active ' => true ],
[ ' user ' => ' fred ' , ' active ' => true ],
[ ' user ' => ' pebbles ' , ' active ' => false ]
]
dropWhile ( $ users , function ( $ user ) { return $ user [ ' active ' ]; } )
// => objects for ['pebbles']每一个
检查predicate是否返回对array的所有元素的真实性。一旦predicate返回虚假,迭代就会停止。谓词带有三个参数:(值,索引,数组)。
注意:此方法对于空数组返回true ,因为对于空数组的元素,一切都是正确的。
参数:
@param itoble $ collection the the to to to to to to to to。
@param callable $ pashitate the tecoke the tecoke the tecoke the迭代调用。
返回:
@return bool true如果所有元素通过谓词检查,则否则false 。
例子:
use function _ every ;
every ([ true , 1 , null , ' yes ' ], function ( $ value ) { return is_bool ( $ value );})
// => 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
此方法就像find ,除了它返回第一个元素谓词的索引返回真相,而不是元素本身。
参数:
@param数组$阵列要检查的数组。
@param callable $ pashitate the tecoke the tecoke the tecoke the迭代调用。
@param int $ fromIndex the Index to搜索。
返回:
@return int找到的元素的索引,else -1 。
例子:
use function _ findIndex ;
$ users = [
[ ' user ' => ' 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 ' );
// => 2FindlastIndex
此方法就像findIndex一样,除了它迭代从右到左的collection元素。
参数:
@param数组$阵列要检查的数组。
@param混合$谓词通过迭代调用功能。
@param int $ fromIndex the Index to搜索。
返回:
@return int找到的元素的索引,else -1 。
例子:
use function _ findLastIndex ;
$ users = [
[ ' user ' => ' barney ' , ' active ' => true ],
[ ' user ' => ' fred ' , ' active ' => false ],
[ ' user ' => ' pebbles ' , ' active ' => false ]
]
findLastIndex ( $ users , function ( $ user ) { return $ user [ ' user ' ] === ' pebbles ' ; })
// => 2扁平
使array平坦一个深度。
参数:
@param数组$阵列the the tht Flatten。
返回:
@Return阵列新的扁平阵列。
例子:
use function _ flatten ;
flatten ([ 1 , [ 2 , [ 3 , [ 4 ]], 5 ]])
// => [1, 2, [3, [4]], 5]Flattendeep
递归平坦的array 。
参数:
@param数组$阵列the the tht Flatten。
返回:
@return阵列返回新的扁平阵列。
例子:
use function _ flattenDeep ;
flattenDeep ([ 1 , [ 2 , [ 3 , [ 4 ]], 5 ]]);
// => [1, 2, 3, 4, 5]Flattendepth
递归使array直至depth时间。
参数:
@param数组$阵列the the tht Flatten。
@param int $深度最大递归深度。
返回:
@Return阵列新的扁平阵列。
例子:
use function _ flattenDepth ;
$ array = [ 1 , [ 2 , [ 3 , [ 4 ]], 5 ]]
flattenDepth ( $ array , 1 )
// => [1, 2, [3, [4]], 5]
flattenDepth ( $ array , 2 )
// => [1, 2, 3, [4], 5]从脚下
toPairs的倒数,此方法返回由键值pairs组成的对象。
参数:
@Param数组$对键值对。
返回:
@return stdclass新对象。
例子:
use function _ fromPairs ;
fromPairs ([[ ' a ' , 1 ], [ ' b ' , 2 ]])
// => stdClass(
// 'a' => 1,
//'b' => 2,
// )头
获取array的第一个元素。
参数:
@param数组$阵列将数组到查询。
返回:
@return混合返回array的第一个元素。
例子:
use function _ head ;
head ([ 1 , 2 , 3 ])
// => 1
head ([])
// => null索引
获取使用SameValueZero在array中发现value首次出现的索引,以进行平等比较。如果fromIndex为负,则将其用作距array末端的偏移。
参数:
@param数组$阵列要检查的数组。
@Param混合$值搜索的值。
@param int $ fromIndex the Index to搜索。
返回:
@return int匹配值的索引,else -1 。
例子:
use function _ indexOf ;
indexOf ([ 1 , 2 , 1 , 2 ], 2 )
// => 1
// Search from the `fromIndex`.
indexOf ([ 1 , 2 , 1 , 2 ], 2 , 2 )
// => 3最初的
获得array的最后一个元素以外的所有内容。
参数:
@param数组$阵列将数组到查询。
返回:
@Return数组array的切片。
例子:
use function _ initial ;
initial ([ 1 , 2 , 3 ])
// => [1, 2]路口
创建一系列独特的值,这些值都包含在所有给定数组中的SameValueZero进行平等比较。结果值的顺序和参考由第一个数组确定。
参数:
@param数组... $阵列
返回:
@return数组相交值的新数组。
例子:
use function _ intersection ;
intersection ([ 2 , 1 ], [ 2 , 3 ])
// => [2]交叉点
此方法就像intersection一样,除了它接受iteratee ,而iTerateE则为每个arrays的每个元素都调用以生成比较它们的标准。结果值的顺序和参考由第一个数组确定。 ITEMERE被一个参数调用:(value)。
参数:
@param数组
@param callable $ iteratee the iteratee每个元素调用。
返回:
@return数组相交值的新数组。
例子:
use function _ intersectionBy ;
intersectionBy ([ 2.1 , 1.2 ], [ 2.3 , 3.4 ], Math.floor)
// => [2.1]
// The `property` iteratee shorthand.
intersectionBy ([[ ' x ' => 1 ]], [[ ' x ' => 2 ], [ ' x ' => 1 ]], ' x ' );
// => [[ 'x' => 1 ]]交点与
此方法就像intersection一样,除了接受comparator以比较arrays元素的比较器。结果值的顺序和参考由第一个数组确定。比较器用两个参数调用:( Arrval,othval)。
参数:
@param数组... $阵列
@param callable $比较器每个元素调用比较器。
返回:
@return数组相交值的新数组。
例子:
use function _ intersectionWith ;
$ objects = [[ ' x ' => 1 , ' y ' => 2 ], [ ' x ' => 2 , ' y ' => 1 ]]
$ others = [[ ' x ' => 1 , ' y ' => 1 ], [ ' x ' => 1 , ' y ' => 2 ]]
intersectionWith ( $ objects , $ others , ' _::isEqual ' )
// => [[ 'x' => 1, 'y' => 2 ]]最后的
获取array的最后一个元素。
参数:
@param数组$阵列将数组到查询。
返回:
@return混合返回array的最后一个元素。
例子:
use function _ last ;
last ([ 1 , 2 , 3 ])
// => 3LastIndexof
此方法就像indexOf ,只是它迭代了从右到左的array元素。
参数:
@param数组$阵列要检查的数组。
@Param混合$值搜索的值。
@param int $ fromIndex the Index to搜索。
返回:
@return int匹配值的索引,else -1 。
例子:
use function _ lastIndexOf ;
lastIndexOf ([ 1 , 2 , 1 , 2 ], 2 )
// => 3
// Search from the `fromIndex`.
lastIndexOf ([ 1 , 2 , 1 , 2 ], 2 , 2 )
// => 1nth
获取array索引n的元素。如果n为负,则返回末端的n个元素。
参数:
@param数组$阵列将数组到查询。
@param int $ n要返回的元素索引。
返回:
@return混合返回array的第n个元素。
例子:
use function _ nth ;
$ array = [ ' a ' , ' b ' , ' c ' , ' d ' ]
nth ( $ array , 1 )
// => 'b'
nth ( $ array , - 2 )
// => 'c'拉
使用SameValueZero从array中删除所有给定的值,以进行平等比较。
注意: without不同,此方法突变array 。使用remove以通过谓词从数组中删除元素。
参数:
@param数组$阵列数组要修改。
@param数组
返回:
@Return数组
例子:
use function _ pull ;
$ array = [ ' a ' , ' b ' , ' c ' , ' a ' , ' b ' , ' c ' ]
pull ( $ array , ' a ' , ' c ' )
var_dump ( $ array )
// => ['b', 'b']Pullall
此方法就像pull一样,除了它接受要删除的值数组。
注意:与difference不同,此方法突变array 。
参数:
@param数组$阵列数组要修改。
@param数组$值要删除的值。
返回:
@return数组array 。
例子:
use function _ pullAll ;
$ array = [ ' a ' , ' b ' , ' c ' , ' a ' , ' b ' , ' c ' ]
pullAll ( $ array , [ ' a ' , ' c ' ])
var_dump ( $ array )
// => ['b', 'b']普拉尔比
此方法就像pullAll一样,除了它接受iteratee ,该iTerateE是为每个array和values的每个元素调用的,以生成比较它们的标准。 ITEMERE被一个参数调用:(value)。
注意:与differenceBy不同,此方法突变array 。
参数:
@param数组$阵列数组要修改。
@param数组$值要删除的值。
@param callable $ iteratee the iteratee每个元素调用。
返回:
@return数组array 。
例子:
use function _ pullAllBy ;
$ array = [[ ' x ' => 1 ], [ ' x ' => 2 ], [ ' x ' => 3 ], [ ' x ' => 1 ]]
pullAllBy ( $ array , [[ ' x ' => 1 ], [ ' x ' => 3 ]], ' x ' )
var_dump ( $ array )
// => [[ 'x' => 2 ]]Pullallwith
此方法就像pullAll一样,除了它接受comparator ,该比较器被调用以比较array与values的元素。比较器用两个参数调用:( Arrval,othval)。
注意:与differenceWith不同,此方法突变array 。
参数:
@param数组$阵列数组要修改。
@param数组$值要删除的值。
@param callable $比较器每个元素调用比较器。
返回:
@return数组array 。
例子:
use function _ pullAllWith ;
$ array = [[ ' x ' => 1 , ' y ' => 2 ], [ ' x ' => 3 , ' y ' => 4 ], [ ' x ' => 5 , ' y ' => 6 ]]
pullAllWith ( $ array , [[ ' x ' => 3 , ' y ' => 4 ]], ' _isEqual ' )
var_dump ( $ array )
// => [[ 'x' => 1, 'y' => 2 ], [ 'x' => 5, 'y' => 6 ]]帕拉特
删除与indexes相对应的array中的元素,并返回一个删除元素的数组。
注意: at不同,此方法突变array 。
参数:
@param数组$阵列数组要修改。
@param(int | int [])$索引要删除的元素索引。
返回:
@return数组删除的新数组。
例子:
use function _ pullAt ;
$ array = [ ' a ' , ' b ' , ' c ' , ' d ' ]
$ pulled = pullAt ( $ array , [ 1 , 3 ])
var_dump ( $ array )
// => ['a', 'c']
var_dump ( $ pulled )
// => ['b', 'd']消除
从array中删除predicate返回真相的所有元素,并返回删除元素的数组。谓词带有三个参数:(值,索引,数组)。
注意:与filter不同,此方法突变array 。使用pull按值从数组中拉出元素。
参数:
@param数组$阵列数组要修改。
@param callable $ pashitate the tecoke the tecoke the tecoke the迭代调用。
返回:
@return数组删除的新数组。
例子:
use function _ remove ;
$ array = [ 1 , 2 , 3 , 4 ]
$ evens = remove ( $ array , function ( $ n ) { return $ n % 2 === 0 ; })
var_dump ( $ array )
// => [1, 3]
var_dump ( $ evens )
// => [2, 4]样本
从array中获取一个随机元素。
参数:
@param数组$阵列示例数组。
返回:
@return混合返回随机元素。
例子:
use function _ sample ;
sample ([ 1 , 2 , 3 , 4 ])
// => 2样品
从array到array的大小,在唯一的键处获取n随机元素。
参数:
@param数组$阵列示例数组。
@param int $ n要采样的元素数量。
返回:
@return数组随机元素。
例子:
use function _ sampleSize ;
sampleSize ([ 1 , 2 , 3 ], 2 )
// => [3, 1]
sampleSize ([ 1 , 2 , 3 ], 4 )
// => [2, 3, 1]洗牌
创建一系列洗牌价值
参数:
@param数组$阵列阵列进行洗牌。
返回:
@return阵列新的洗牌阵列。
通过命令行克隆项目: