lodash php

2025-12-07 0 947

lodashphp

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中的每种方法都是一个单独的功能,可以单独导入和使用。

 <?php

use function _ \\ each ;

each ([ 1 , 2 , 3 ], function ( int $ item ) {
    var_dump ( $ item );
});

Lodash-PHP还带有一个全球使用的全局_类。

 <?php

_:: each ([ 1 , 2 , 3 ], function ( int $ item ) {
    var_dump ( $ item );
});

方法

  • 大批
  • 收藏
  • 日期
  • 功能
  • 数学
  • 数字
  • 目的
  • seq
  • 细绳
  • util

大批

大块

创建一系列元素分为size的长度。如果array不能均匀分开,则最终的块将是其余元素。

参数:

@Param数组$阵列数组阵列要处理。

@param int $ [size = 1]每个块的长度

返回:

@return数组返回新的块。

例子:

 <?php
 use function _ \\ chunk ;

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

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

袖珍的

创建一个删除所有假值的数组。值falsenull0""undefined ”和NaN是错误的。

参数:

@param数组$阵列compact。

返回:

@return数组返回过滤值的新数组。

例子:

 <?php
 use function _ \\ compact ;

compact ([ 0 , 1 , false , 2 , \'\' , 3 ])
// => [1, 2, 3]

concat

与任何其他数组和/或值创建一个新的数组串联array

参数:

@param数组$阵列将数组与连接酸盐。

@param数组<int,混合> $值condenate的值。

返回:

@return数组返回新的串联阵列。

例子:

 <?php
 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数组返回过滤值的新数组。

例子:

 <?php
 use function _ \\ difference ;

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

差异

此方法就像difference一样,除了它接受iteratee ,该iTerateE是为array的每个元素和values所调用的,以生成比较它们的标准。结果值的顺序和参考由第一个数组确定。 ITEMERE被一个参数调用:(value)。

注意:pullAllBy不同,此方法返回一个新数组。

参数:

@param数组$阵列要检查的数组。

@param数组<int,混合> … $值排除的值。

@param callable $ iteratee the iteratee每个元素调用。

返回:

@return数组返回过滤值的新数组。

例子:

 <?php
 use function _ \\ differenceBy ;

differenceBy ([ 2.1 , 1.2 ], [ 2.3 , 3.4 ], \' floor \' )
// => [1.2]

差异

此方法就像difference一样,除了它接受comparator ,该比较器被调用以将array的元素与values进行比较。结果值的顺序和参考由第一个数组确定。比较器用两个参数调用:( Arrval,othval)。

注意:pullAllWith不同,此方法返回一个新数组。

参数:

@param数组<int,混合> $ rarray the待检查的数组。

@param数组… $值排除的值。

@param callable $比较器每个元素调用比较器。

返回:

@return数组返回过滤值的新数组。

例子:

 <?php
 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的切片。

例子:

 <?php
 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的切片。

例子:

 <?php
 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的切片。

例子:

 <?php
 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的切片。

例子:

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

例子:

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

例子:

 <?php
 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 \' );
// => 2

FindlastIndex

此方法就像findIndex一样,除了它迭代从右到左的collection元素。

参数:

@param数组$阵列要检查的数组。

@param混合$谓词通过迭代调用功能。

@param int $ fromIndex the Index to搜索。

返回:

@return int找到的元素的索引,else -1

例子:

 <?php
 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阵列新的扁平阵列。

例子:

 <?php
 use function _ \\ flatten ;

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

Flattendeep

递归平坦的array

参数:

@param数组$阵列the the tht Flatten。

返回:

@return阵列返回新的扁平阵列。

例子:

 <?php
 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阵列新的扁平阵列。

例子:

 <?php
 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新对象。

例子:

 <?php
 use function _ \\ fromPairs ;

fromPairs ([[ \' a \' , 1 ], [ \' b \' , 2 ]])
// => stdClass(
// \'a\' => 1,
//\'b\' => 2,
// )

获取array的第一个元素。

参数:

@param数组$阵列将数组到查询。

返回:

@return混合返回array的第一个元素。

例子:

 <?php
 use function _ \\ head ;

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

head ([])
// => null

索引

获取使用SameValueZeroarray中发现value首次出现的索引,以进行平等比较。如果fromIndex为负,则将其用作距array末端的偏移。

参数:

@param数组$阵列要检查的数组。

@Param混合$值搜索的值。

@param int $ fromIndex the Index to搜索。

返回:

@return int匹配值的索引,else -1

例子:

 <?php
 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的切片。

例子:

 <?php
 use function _ \\ initial ;

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

路口

创建一系列独特的值,这些值都包含在所有给定数组中的SameValueZero进行平等比较。结果值的顺序和参考由第一个数组确定。

参数:

@param数组… $阵列

返回:

@return数组相交值的新数组。

例子:

 <?php
 use function _ \\ intersection ;

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

交叉点

此方法就像intersection一样,除了它接受iteratee ,而iTerateE则为每个arrays的每个元素都调用以生成比较它们的标准。结果值的顺序和参考由第一个数组确定。 ITEMERE被一个参数调用:(value)。

参数:

@param数组<int,混合> … $ arrays

@param callable $ iteratee the iteratee每个元素调用。

返回:

@return数组相交值的新数组。

例子:

 <?php
 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数组相交值的新数组。

例子:

 <?php
 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的最后一个元素。

例子:

 <?php
 use function _ \\ last ;

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

LastIndexof

此方法就像indexOf ,只是它迭代了从右到左的array元素。

参数:

@param数组$阵列要检查的数组。

@Param混合$值搜索的值。

@param int $ fromIndex the Index to搜索。

返回:

@return int匹配值的索引,else -1

例子:

 <?php
 use function _ \\ lastIndexOf ;

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

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

nth

获取array索引n的元素。如果n为负,则返回末端的n个元素。

参数:

@param数组$阵列将数组到查询。

@param int $ n要返回的元素索引。

返回:

@return混合返回array的第n个元素。

例子:

 <?php
 use function _ \\ nth ;

$ array = [ \' a \' , \' b \' , \' c \' , \' d \' ]

nth ( $ array , 1 )
// => \'b\'

nth ( $ array , - 2 )
// => \'c\'

使用SameValueZeroarray中删除所有给定的值,以进行平等比较。

注意: without不同,此方法突变array 。使用remove以通过谓词从数组中删除元素。

参数:

@param数组$阵列数组要修改。

@param数组<int,字符串> $值要删除的值。

返回:

@Return数组

例子:

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

例子:

 <?php
 use function _ \\ pullAll ;

$ array = [ \' a \' , \' b \' , \' c \' , \' a \' , \' b \' , \' c \' ]

pullAll ( $ array , [ \' a \' , \' c \' ])
var_dump ( $ array )
// => [\'b\', \'b\']

普拉尔比

此方法就像pullAll一样,除了它接受iteratee ,该iTerateE是为每个arrayvalues的每个元素调用的,以生成比较它们的标准。 ITEMERE被一个参数调用:(value)。

注意:differenceBy不同,此方法突变array

参数:

@param数组$阵列数组要修改。

@param数组$值要删除的值。

@param callable $ iteratee the iteratee每个元素调用。

返回:

@return数组array

例子:

 <?php
 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 ,该比较器被调用以比较arrayvalues的元素。比较器用两个参数调用:( Arrval,othval)。

注意:differenceWith不同,此方法突变array

参数:

@param数组$阵列数组要修改。

@param数组$值要删除的值。

@param callable $比较器每个元素调用比较器。

返回:

@return数组array

例子:

 <?php
 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数组删除的新数组。

例子:

 <?php
 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数组删除的新数组。

例子:

 <?php
 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混合返回随机元素。

例子:

 <?php
 use function _ \\ sample ;

sample ([ 1 , 2 , 3 , 4 ])
// => 2

样品

arrayarray的大小,在唯一的键处获取n随机元素。

参数:

@param数组$阵列示例数组。

@param int $ n要采样的元素数量。

返回:

@return数组随机元素。

例子:

 <?php
 use function _ \\ sampleSize ;

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

sampleSize ([ 1 , 2 , 3 ], 4 )
// => [2, 3, 1]

洗牌

创建一系列洗牌价值

参数:

@param数组$阵列阵列进行洗牌。

返回:

@return阵列新的洗牌阵列。

<p dir=

下载源码

通过命令行克隆项目:

git clone https://github.com/lodash-php/lodash-php.git

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 开发教程 lodash php https://www.zuozi.net/32030.html

Gopherus
上一篇: Gopherus
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务