Lodash-PHP 是 Lodash JS 库到 PHP 的端口。它是一组易于使用的实用函数,适用于日常 PHP 项目。
Lodash-PHP 尝试尽可能模仿 lodash.js
Lodash-PHP 至少需要 PHP 7.2+,但始终建议使用最新版本的 PHP。
通过composer安装Lodash-PHP:
$ composer require lodash-php/lodash-phpLodash-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 );
});创建一个元素数组,分为长度为size的组。如果array不能均匀分割,则最终的块将是剩余的元素。
论据:
@param array $array array 要处理的数组。
@param int $number [size=1] 每个块的长度
返回:
@return array 返回新的块数组。
例子:
<?php
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为 false。
论据:
@param array $array 要压缩的数组。
返回:
@return array 返回过滤值的新数组。
例子:
<?php
use function _ compact ;
compact ([ 0 , 1 , false , 2 , '' , 3 ])
// => [1, 2, 3]创建一个新数组,将array与任何其他数组和/或值连接起来。
论据:
@param array $array 要连接的数组。
@param array<int, mix> $values 要连接的值。
返回:
@return array 返回新的串联数组。
例子:
<?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 array $array 要检查的数组。
@param array ...$values 要排除的值。
返回:
@return array 返回过滤值的新数组。
例子:
<?php
use function _ difference ;
difference ([ 2 , 1 ], [ 2 , 3 ])
// => [1]此方法与difference类似,只是它接受为array和values的每个元素调用的iteratee ,以生成比较它们的标准。结果值的顺序和引用由第一个数组确定。迭代器通过一个参数调用:(值)。
注意:与pullAllBy不同,此方法返回一个新数组。
论据:
@param array $array 要检查的数组。
@param array<int, mix> ...$values 要排除的值。
@param callable $iteratee 每个元素调用的 iteratee。
返回:
@return array 返回过滤值的新数组。
例子:
<?php
use function _ differenceBy ;
differenceBy ([ 2.1 , 1.2 ], [ 2.3 , 3.4 ], ' floor ' )
// => [1.2]此方法与difference类似,只是它接受comparator ,调用该比较器将array的元素与values进行比较。结果值的顺序和引用由第一个数组确定。使用两个参数调用比较器:(arrVal, othVal)。
注意:与pullAllWith不同,此方法返回一个新数组。
论据:
@param array<int, mix> $array 要检查的数组。
@param array ...$values 要排除的值。
@param callable $comparator 每个元素调用的比较器。
返回:
@return array 返回过滤值的新数组。
例子:
<?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 array $array 要查询的数组。
@param int $n 要删除的元素数量。
返回:
@return array 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 array $array 要查询的数组。
@param int $n 要删除的元素数量。
返回:
@return array 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返回 falsey。使用三个参数调用谓词:(值、索引、数组)。
论据:
@param array $array 要查询的数组。
@param callable $predicate 每次迭代调用的函数。
返回:
@return array 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返回 falsey。使用三个参数调用谓词:(值、索引、数组)。
论据:
@param array $array 要查询的数组。
@param callable $predicate 每次迭代调用的函数。
返回:
@return array 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的所有元素返回 true 。一旦predicate返回 false,迭代就会停止。使用三个参数调用谓词:(值、索引、数组)。
注意:此方法对于空数组返回true因为空数组的元素的所有内容都为 true。
论据:
@param iterable $collection 要迭代的数组。
@param callable $predicate 每次迭代调用的函数。
返回:
@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
此方法与find类似,只不过它返回第一个元素的索引,谓词返回 true for 而不是元素本身。
论据:
@param array $array 要检查的数组。
@param callable $predicate 每次迭代调用的函数。
@param int $fromIndex 要搜索的索引。
返回:
@return int 找到的元素的索引,否则-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此方法类似于findIndex ,只不过它从右到左迭代collection的元素。
论据:
@param array $array 要检查的数组。
@param mix $predicate 每次迭代调用的函数。
@param int $fromIndex 要搜索的索引。
返回:
@return int 找到的元素的索引,否则-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 array $array 要展平的数组。
返回:
@return array 新的扁平化数组。
例子:
<?php
use function _ flatten ;
flatten ([ 1 , [ 2 , [ 3 , [ 4 ]], 5 ]])
// => [1, 2, [3, [4]], 5]递归地展平array 。
论据:
@param array $array 要展平的数组。
返回:
@return array 返回新的展平数组。
例子:
<?php
use function _ flattenDeep ;
flattenDeep ([ 1 , [ 2 , [ 3 , [ 4 ]], 5 ]]);
// => [1, 2, 3, 4, 5]递归地将array展平至depth倍。
论据:
@param array $array 要展平的数组。
@param int $depth 最大递归深度。
返回:
@return array 新的扁平化数组。
例子:
<?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 array $pairs 键值对。
返回:
@return stdClass 新对象。
例子:
<?php
use function _ fromPairs ;
fromPairs ([[ ' a ' , 1 ], [ ' b ' , 2 ]])
// => stdClass(
// 'a' => 1,
//'b' => 2,
// )获取array的第一个元素。
论据:
@param array $array 要查询的数组。
返回:
@return mix 返回array的第一个元素。
例子:
<?php
use function _ head ;
head ([ 1 , 2 , 3 ])
// => 1
head ([])
// => null使用SameValueZero进行相等比较,获取在array中第一次出现value的索引。如果fromIndex为负数,则将其用作距array末尾的偏移量。
论据:
@param array $array 要检查的数组。
@param mix $value 要搜索的值。
@param int $fromIndex 要搜索的索引。
返回:
@return int 匹配值的索引,否则-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 array $array 要查询的数组。
返回:
@return array array的切片。
例子:
<?php
use function _ initial ;
initial ([ 1 , 2 , 3 ])
// => [1, 2]使用SameValueZero进行相等比较,创建包含在所有给定数组中的唯一值的数组。结果值的顺序和引用由第一个数组确定。
论据:
@param数组...$数组
返回:
@return array 新的相交值数组。
例子:
<?php
use function _ intersection ;
intersection ([ 2 , 1 ], [ 2 , 3 ])
// => [2]此方法类似于intersection ,只不过它接受为每个arrays的每个元素调用的iteratee ,以生成比较它们的标准。结果值的顺序和引用由第一个数组确定。迭代器通过一个参数调用:(值)。
论据:
@param array<int, 混合> ...$arrays
@param callable $iteratee 每个元素调用的 iteratee。
返回:
@return array 新的相交值数组。
例子:
<?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 $comparator 每个元素调用的比较器。
返回:
@return array 新的相交值数组。
例子:
<?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 array $array 要查询的数组。
返回:
@return mix 返回array的最后一个元素。
例子:
<?php
use function _ last ;
last ([ 1 , 2 , 3 ])
// => 3此方法类似于indexOf ,只不过它从右到左迭代array的元素。
论据:
@param array $array 要检查的数组。
@param mix $value 要搜索的值。
@param int $fromIndex 要搜索的索引。
返回:
@return int 匹配值的索引,否则-1 。
例子:
<?php
use function _ lastIndexOf ;
lastIndexOf ([ 1 , 2 , 1 , 2 ], 2 )
// => 3
// Search from the `fromIndex`.
lastIndexOf ([ 1 , 2 , 1 , 2 ], 2 , 2 )
// => 1获取array的索引n处的元素。如果n为负数,则返回倒数第 n 个元素。
论据:
@param array $array 要查询的数组。
@param int $n 要返回的元素的索引。
返回:
@return mix 返回array的第 n 个元素。
例子:
<?php
use function _ nth ;
$ array = [ ' a ' , ' b ' , ' c ' , ' d ' ]
nth ( $ array , 1 )
// => 'b'
nth ( $ array , - 2 )
// => 'c'使用SameValueZero进行相等比较,从array中删除所有给定值。
注意:与without不同,此方法会改变array 。使用remove通过谓词从数组中删除元素。
论据:
@param array $array 要修改的数组。
@param array<int, string> $values 要删除的值。
返回:
@返回数组
例子:
<?php
use function _ pull ;
$ array = [ ' a ' , ' b ' , ' c ' , ' a ' , ' b ' , ' c ' ]
pull ( $ array , ' a ' , ' c ' )
var_dump ( $ array )
// => ['b', 'b']此方法与pull类似,只不过它接受要删除的值数组。
注意:与difference不同,此方法会改变array 。
论据:
@param array $array 要修改的数组。
@param array $values 要删除的值。
返回:
@返回数组array 。
例子:
<?php
use function _ pullAll ;
$ array = [ ' a ' , ' b ' , ' c ' , ' a ' , ' b ' , ' c ' ]
pullAll ( $ array , [ ' a ' , ' c ' ])
var_dump ( $ array )
// => ['b', 'b']此方法类似于pullAll只不过它接受为array和values的每个元素调用的iteratee ,以生成比较它们的标准。迭代器通过一个参数调用:(值)。
注意:与differenceBy不同,此方法会改变array 。
论据:
@param array $array 要修改的数组。
@param array $values 要删除的值。
@param callable $iteratee 每个元素调用的 iteratee。
返回:
@返回数组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 ]]此方法与pullAll类似,只是它接受comparator ,调用该比较器将array元素与values进行比较。使用两个参数调用比较器:(arrVal, othVal)。
注意:与differenceWith不同,此方法会改变array 。
论据:
@param array $array 要修改的数组。
@param array $values 要删除的值。
@param callable $comparator 每个元素调用的比较器。
返回:
@返回数组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 ]]从array中删除与indexes对应的元素并返回删除元素的数组。
注意:与at不同,此方法会改变array 。
论据:
@param array $array 要修改的数组。
@param (int | int[]) $indexes 要删除的元素的索引。
返回:
@return array 删除元素的新数组。
例子:
<?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返回 true 的所有元素,并返回已删除元素的数组。使用三个参数调用谓词:(值、索引、数组)。
注意:与filter不同,此方法会改变array 。使用pull按值从数组中提取元素。
论据:
@param array $array 要修改的数组。
@param callable $predicate 每次迭代调用的函数。
返回:
@return array 删除元素的新数组。
例子:
<?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 array $array 要采样的数组。
返回:
@return mix 返回随机元素。
例子:
<?php
use function _ sample ;
sample ([ 1 , 2 , 3 , 4 ])
// => 2从array唯一键处获取n随机元素,直到array的大小。
论据:
@param array $array 要采样的数组。
@param int $n 要采样的元素数量。
返回:
@return 数组随机元素。
例子:
<?php
use function _ sampleSize ;
sampleSize ([ 1 , 2 , 3 ], 2 )
// => [3, 1]
sampleSize ([ 1 , 2 , 3 ], 4 )
// => [2, 3, 1]创建一个打乱值的数组
论据:
@param array $array 要洗牌的数组。
返回:
@return array 新的打乱数组。
例子:
<?php
use function _ shuffle ;
shuffle ([ 1 , 2 , 3 , 4 ])
// => [4, 1, 3, 2]创建从start到(但不包括end的array切片。
论据:
@param array $array 要切片的数组。
@param int $start 开始位置。
@param int $end 结束位置。
返回:
@return array array的切片。
获取array中除第一个元素之外的所有元素。
论据:
@param array $array 要查询的数组。
返回:
@return array array的切片。
例子:
<?php
use function _ tail ;
tail ([ 1 , 2 , 3 ])
// => [2, 3]创建一个array切片,其中包含从头开始的n元素。
论据:
@param array $array 要查询的数组。
@param int $n 要获取的元素数量。
返回:
@return array array的切片。
例子:
<?php
use function _ take ;
take ([ 1 , 2 , 3 ])
// => [1]
take ([ 1 , 2 , 3 ], 2 )
// => [1, 2]
take ([ 1 , 2 , 3 ], 5 )
// => [1, 2, 3]
take ([ 1 , 2 , 3 ], 0 )
// => []创建一个array切片,其中包含从末尾取出的n元素。
论据:
@param array $array 要查询的数组。
@param int $n 要获取的元素数量。
返回:
@return array array的切片。
例子:
<?php
use function _ takeRight ;
takeRight ([ 1 , 2 , 3 ])
// => [3]
takeRight ([ 1 , 2 , 3 ], 2 )
// => [2, 3]
takeRight ([ 1 , 2 , 3 ], 5 )
// => [1, 2, 3]
takeRight ([ 1 , 2 , 3 ], 0 )
// => []创建一个array切片,其中的元素取自末尾。获取元素直到predicate返回 falsey。使用三个参数调用谓词:(值、索引、数组)。
论据:
@param array $array 要查询的数组。
@param callable $predicate 每次迭代调用的函数。
返回:
@return array array的切片。
例子:
<?php
use function _ takeRightWhile ;
$ users = [
[ ' user ' => ' barney ' , ' active ' => false ],
[ ' user ' => ' fred ' , ' active ' => true ],
[ ' user ' => ' pebbles ' , ' active ' => true ]
];
takeRightWhile ( $ users , function ( $ value ) { return $ value [ ' active ' ]; })
// => objects for ['fred', 'pebbles']使用从开头获取的元素创建array切片。获取元素直到predicate返回 falsey。使用三个参数调用谓词:(值、索引、数组)。
论据:
@param array $array 要查询的数组。
@param mix $predicate 每次迭代调用的函数。
返回:
@return array array的切片。
例子:
<?php
use function _ takeWhile ;
$ users = [
[ ' user ' => ' barney ' , ' active ' => true ],
[ ' user ' => ' fred ' , ' active ' => true ],
[ ' user ' => ' pebbles ' , ' active ' => false ]
]
takeWhile ( $ users , function ( $ value ) { return $ value [ ' active ' ]; })
// => objects for ['barney', 'fred']使用SameValueZero进行相等比较,从所有给定数组中按顺序创建唯一值数组。
论据:
@param array ...$arrays 要检查的数组。
返回:
@return array 新的组合值数组。
例子:
<?php
use function _ union ;
union ([ 2 ], [ 1 , 2 ])
// => [2, 1]此方法类似于union ,只不过它接受为每个arrays的每个元素调用的iteratee ,以生成计算唯一性的标准。结果值是从该值出现的第一个数组中选择的。迭代器通过一个参数调用:(值)。
论据:
@param array<int, mix> ...$arrays 要检查的数组。
@param callable $iteratee 每个元素调用的 iteratee。
返回:
@return array 新的组合值数组。
例子:
<?php
use function _ unionBy ;
unionBy ([ 2.1 ], [ 1.2 , 2.3 ], ' floor ' )
// => [2.1, 1.2]
// The `_::property` iteratee shorthand.
unionBy ([[ ' x ' => 1 ]], [[ ' x ' => 2 ], [ ' x ' => 1 ]], ' x ' );
// => [['x' => 1], ['x' => 2]]此方法类似于union ,只不过它接受comparator ,该比较器被调用来比较arrays的元素。结果值是从该值出现的第一个数组中选择的。使用两个参数调用比较器:(arrVal, othVal)。
论据:
@param array<int, mix> ...$arrays 要检查的数组。
@param callable $comparator 每个元素调用的比较器。
返回:
@return array 新的组合值数组。
例子:
<?php
use function _ unionWith ;
$ objects = [[ ' x ' => 1 , ' y ' => 2 ], [ ' x ' => 2 , ' y ' => 1 ]]
$ others = [[ ' x ' => 1 , ' y ' => 1 ], [ ' x ' => 1 , ' y ' => 2 ]]
unionWith ( $ objects , $ others , ' _::isEqual ' )
// => [['x' => 1, 'y' => 2], ['x' => 2, 'y' => 1], ['x' => 1, 'y' => 1]]创建数组的无重复版本,使用SameValueZero进行相等比较,其中仅保留每个元素的第一次出现。结果值的顺序由它们在数组中出现的顺序决定。
论据:
@param array $array 要检查的数组。
返回:
@return array 新的重复的空闲数组。
例子:
<?php
use function _ uniq ;
uniq ([ 2 , 1 , 2 ])
// => [2, 1]s此方法类似于uniq只不过它接受为array中的每个元素调用的iteratee ,以生成计算唯一性的标准。结果值的顺序由它们在数组中出现的顺序决定。迭代器通过一个参数调用:(值)。
论据:
@param array $array 要检查的数组。
@param mix $iteratee 每个元素调用的 iteratee。
返回:
@return array 新的重复的空闲数组。
例子:
<?php
use function _ uniqBy ;
uniqBy ([ 2.1 , 1.2 , 2.3 ], ' floor ' )
// => [2.1, 1.2]此方法类似于uniq只不过它接受comparator ,该比较器被调用来比较array的元素。结果值的顺序由它们在数组中出现的顺序决定。使用两个参数调用比较器:(arrVal, othVal)。
论据:
@param array $array 要检查的数组。
@param callable $comparator 每个元素调用的比较器。
返回:
@return array 新的重复的空闲数组。
例子:
<?php
use function _ uniqWith ;
$ objects = [[ ' x ' => 1 , ' y ' => 2 ], [ ' x ' => 2 , ' y ' => 1 ], [ ' x ' => 1 , ' y ' => 2 ]]
uniqWith ( $ objects , ' _::isEqual ' )
// => [['x' => 1, 'y' => 2], ['x' => 2, 'y' => 1]]此方法类似于zip不同之处在于它接受分组元素的数组,并创建一个将元素重新分组为其压缩前配置的数组。
论据:
@param array $array 要处理的分组元素的数组。
返回:
@return array 重组元素的新数组。
例子:
<?php
use function _ unzip ;
$ zipped = zip ([ ' a ' , ' b ' ], [ 1 , 2 ], [ true , false ])
// => [['a', 1, true], ['b', 2, false]]
unzip ( $ zipped )
// => [['a', 'b'], [1, 2], [true, false]]此方法类似于unzip只不过它接受iteratee来指定如何组合重新分组的值。使用每个组的元素调用迭代器:(...group)。
论据:
@param array $array 要处理的分组元素的数组。
@param (callable | null) $iteratee 组合重组值的函数。
返回:
@return array 重组元素的新数组。
例子:
<?php
use function _ unzipWith ;
$ zipped = zip ([ 1 , 2 ], [ 10 , 20 ], [ 100 , 200 ])
// => [[1, 10, 100], [2, 20, 200]]
unzipWith (zipped, ' _::add ' )
// => [3, 30, 300]使用SameValueZero创建一个排除所有给定值的数组以进行相等比较。
注意:与pull不同,此方法返回一个新数组。
论据:
@param array $array 要检查的数组。
@param array<int, mix> $values 要排除的值。
返回:
@return array 过滤值的新数组。
例子:
<?php
use function _ without ;
without ([ 2 , 1 , 2 , 3 ], 1 , 2 )
// => [3]创建一个分组元素数组,其中第一个包含给定数组的第一个元素,第二个包含给定数组的第二个元素,依此类推。
论据:
@param array ...$arrays 要处理的数组。
返回:
@return array 分组元素的新数组。
例子:
<?php
use function _ zip ;
zip ([ ' a ' , ' b ' ], [ 1 , 2 ], [ true , false ])
// => [['a', 1, true], ['b', 2, false]]此方法类似于fromPairs不同之处在于它接受两个数组,一个属性标识符和一个对应值。
论据:
@param array $props 属性标识符。
@param array $values 属性值。
返回:
@return object 新对象。
例子:
<?php
use function _ zipObject ;
zipObject ([ ' a ' , ' b ' ], [ 1 , 2 ])
/* => object(stdClass) #210 (2) {
[ " a " ] => int( 1 )
[ " b " ] => int( 2 )
}
*/此方法类似于zipObject只不过它支持属性路径。
论据:
@param array $props 属性标识符。
@param array $values 属性值。
返回:
@return stdClass 新对象。
例子:
<?php
use function _ zipObjectDeep ;
zipObjectDeep ([ ' a.b[0].c ' , ' a.b[1].d ' ], [ 1 , 2 ])
/* => class stdClass #20 (1) {
public $ a => class stdClass #19 (1) {
public $ b =>
array ( 2 ) {
[ 0 ] => class stdClass #17 (1) {
public $ c => int( 1 )
}
[ 1 ] => class stdClass #18 (1) {
public $ d => int( 2 )
}
}
}
}
*/此方法类似于zip只不过它接受iteratee来指定如何组合分组值。使用每个组的元素调用迭代器:(...group)。
论据:
@param array<int, (array | callable)> ...$arrays 要处理的数组。
@param callable $iteratee 组合分组值的函数。
返回:
@return array 分组元素的新数组。
例子:
<?php
use function _ zipWith ;
zipWith ([ 1 , 2 ], [ 10 , 20 ], [ 100 , 200 ], function ( $ a , $ b , $ c ) { return $ a + $ b + $ c ; })
// => [111, 222] 创建一个由通过iteratee运行collection中每个元素的结果生成的键组成的数组。每个键对应的值是iteratee返回该键的次数。使用一个参数调用迭代器:(值)。
论据:
@param iterable $collection 要迭代的集合。
@param callable $iteratee 用于转换键的 iteratee。
返回:
@return array 返回组合的聚合对象。
例子:
<?php
use function _ countBy ;
countBy ([ 6.1 , 4.2 , 6.3 ], ' floor ' );
// => ['6' => 2, '4' => 1]
// The `property` iteratee shorthand.
countBy ([ ' one ' , ' two ' , ' three ' ], ' strlen ' );
// => ['3' => 2, '5' => 1]迭代collection的元素并为每个元素调用iteratee 。使用三个参数调用迭代器:(值、索引|键、集合)。 Iteratee 函数可以通过显式返回false来提前退出迭代。
注意:与其他“Collections”方法一样,具有“length”属性的对象会像数组一样进行迭代。为了避免这种行为,请使用forIn或forOwn进行对象迭代。
论据:
@param (array | iterable | object) $collection 要迭代的集合。
@param callable $iteratee 每次迭代调用的函数。
返回:
@return (array | object) 返回collection 。
例子:
<?php
use function _ each ;
each ([ 1 , 2 ], function ( $ value ) { echo $ value ; })
// => Echoes `1` then `2`.
each (( object ) [ ' a ' => 1 , ' b ' => 2 ], function ( $ value , $ key ) { echo $ key ; });
// => Echoes 'a' then 'b' (iteration order is not guaranteed).此方法与each类似,只是它从右到左迭代collection的元素。
论据:
@param (array | iterable | object) $collection 要迭代的集合。
@param callable $iteratee 每次迭代调用的函数。
返回:
@return (array | object) 返回collection 。
例子:
<?php
use function _ eachRight ;
eachRight ([ 1 , 2 ], function ( $ value ) { echo $ value ; })
// => Echoes `2` then `1`.迭代array的元素,返回所有元素的数组predicate返回 true for。使用三个参数调用谓词:(值、索引、数组)。
注意:与remove不同,此方法返回一个新数组。
论据:
@param iterable $array 要迭代的数组。
@param callable $predicate 每次迭代调用的函数。
返回:
@return array 新的过滤数组。
例子:
<?php
use function _ filter ;
$ users = [
[ ' user ' => ' barney ' , ' age ' => 36 , ' active ' => true ],
[ ' user ' => ' fred ' , ' age ' => 40 , ' active ' => false ]
];
filter ( $ users , function ( $ o ) { return ! $ o [ ' active ' ]; });
// => objects for ['fred']
// The `matches` iteratee shorthand.
filter ( $ users , [ ' age ' => 36 , ' active ' => true ]);
// => objects for ['barney']
// The `matchesProperty` iteratee shorthand.
filter ( $ users , [ ' active ' , false ]);
// => objects for ['fred']
// The `property` iteratee shorthand.
filter ( $ users , ' active ' );
// => objects for ['barney']迭代collection的元素,返回第一个元素predicate返回 true for。使用三个参数调用谓词:(值、索引|键、集合)。
论据:
@param iterable $collection 要检查的集合。
@param callable $predicate 每次迭代调用的函数。
@param int $fromIndex 要搜索的索引。
返回:
@return mix 返回匹配的元素,否则null 。
例子:
<?php
use function _ find ;
$ users = [
[ ' user ' => ' barney ' , ' age ' => 36 , ' active ' => true ],
[ ' user ' => ' fred ' , ' age ' => 40 , ' active ' => false ],
[ ' user ' => ' pebbles ' , ' age ' => 1 , ' active ' => true ]
];
find ( $ users , function ( $ o ) { return $ o [ ' age ' ] < 40 ; });
// => object for 'barney'
// The `matches` iteratee shorthand.
find ( $ users , [ ' age ' => 1 , ' active ' => true ]);
// => object for 'pebbles'
// The `matchesProperty` iteratee shorthand.
find ( $ users , [ ' active ' , false ]);
// => object for 'fred'
// The `property` iteratee shorthand.
find ( $ users , ' active ' );
// => object for 'barney'此方法类似于find ,只不过它从右到左迭代collection的元素。
论据:
@param iterable $collection 要检查的集合。
@param callable $predicate 每次迭代调用的函数。
@param int $fromIndex 要搜索的索引。
返回:
@return mix 返回匹配的元素,否则返回undefined 。
例子:
<?php
use function _ findLast ;
findLast ([ 1 , 2 , 3 , 4 ], function ( $ n ) { return $ n % 2 == 1 ; })
// => 3通过iteratee运行collection中的每个元素并展平映射结果,创建展平的值数组。使用三个参数调用迭代器:(值、索引|键、集合)。
论据:
@param iterable $collection 要迭代的集合。
@param callable $iteratee 每次迭代调用的函数。
返回:
@return array 新的扁平化数组。
例子:
<?php
use function _ flatMap ;
function duplicate ( $ n ) {
return [ $ n , $ n ]
}
flatMap ([ 1 , 2 ], ' duplicate ' )
// => [1, 1, 2, 2]此方法类似于flatMap只不过它递归地展平映射结果。
论据:
@param iterable $collection 要迭代的集合。
@param callable $iteratee 每次迭代调用的函数。
返回:
@return array 返回新的展平数组。
例子:
<?php
use function _ flatMapDeep ;
function duplicate ( $ n ) {
return [[[ $ n , $ n ]]];
}
flatMapDeep ([ 1 , 2 ], ' duplicate ' );
// => [1, 1, 2, 2]此方法类似于flatMap ,只不过它递归地将映射结果展平至depth倍。
论据:
@param iterable $collection 要迭代的集合。
@param callable $iteratee 每次迭代调用的函数。
@param int $depth 最大递归深度。
返回:
@return array 新的扁平化数组。
例子:
<?php
use function _ flatMapDepth ;
function duplicate ( $ n ) {
return [[[ $ n , $ n ]]]
}
flatMapDepth ([ 1 , 2 ], ' duplicate ' , 2 )
// => [[1, 1], [2, 2]]创建一个由通过iteratee运行collection的每个元素的结果生成的键组成的数组。分组值的顺序由它们在collection中出现的顺序决定。每个键对应的值是负责生成键的元素数组。迭代器通过一个参数调用:(值)。
论据:
@param iterable $collection 要迭代的集合。
@param callable $iteratee 用于转换键的 iteratee。
返回:
@return array 返回组合的聚合对象。
例子:
<?php
use function _ groupBy ;
groupBy ([ 6.1 , 4.2 , 6.3 ], ' floor ' );
// => ['6' => [6.1, 6.3], '4' => [4.2]]
groupBy ([ ' one ' , ' two ' , ' three ' ], ' strlen ' );
// => ['3' => ['one', 'two'], '5' => ['three']]调用collection中每个元素的path处的方法,返回每个调用方法的结果的数组。向每个调用的方法提供任何附加参数。如果path是一个函数,则会为collection中的每个元素调用它,并且this绑定到该函数。
论据:
@param iterable $collection 要迭代的集合。
@param (array | callable | string) $path 要调用的方法或每次迭代调用的函数的路径。
@param array $args 调用每个方法的参数。
返回:
@return array 结果数组。
例子:
<?php
use function _ invokeMap ;
invokeMap ([[ 5 , 1 , 7 ], [ 3 , 2 , 1 ]], function ( $ result ) { sort ( $ result ); return $ result ;})
// => [[1, 5, 7], [1, 2, 3]]
invokeMap ([ 123 , 456 ], ' str_split ' )
// => [['1', '2', '3'], ['4', '5', '6']]创建一个由通过iteratee运行collection中每个元素的结果生成的键组成的对象。每个键对应的值是负责生成该键的最后一个元素。迭代器通过一个参数调用:(值)。
论据:
@param iterable $collection 要迭代的集合。
@param callable $iteratee 用于转换键的 iteratee。
返回:
@return array 组成的聚合对象。
例子:
<?php
use function _ keyBy ;
$ array = [
[ ' direction ' => ' left ' , ' code ' => 97 ],
[ ' direction ' => ' right ' , ' code ' => 100 ],
];
keyBy ( $ array , function ( $ o ) { return chr ( $ o [ ' code ' ]); })
// => ['a' => ['direction' => 'left', 'code' => 97], 'd' => ['direction' => 'right', 'code' => 100]]
keyBy ( $ array , ' direction ' );
// => ['left' => ['direction' => 'left', 'code' => 97], 'right' => ['direction' => 'right', 'code' => 100]]通过iteratee运行collection中的每个元素来创建值数组。使用三个参数调用迭代器:(值、索引|键、集合)。
许多 lodash-php 方法都被保护为_::every 、 _::filter 、 _::map 、 _::mapValues 、 _::reject和_::some等方法的迭代器。
受保护的方法有: ary 、 chunk 、 curry 、 curryRight 、 drop 、 dropRight 、 every 、 fill 、 invert 、 parseInt 、 random 、 range 、 rangeRight 、 repeat 、 sampleSize 、 slice 、 some 、 sortBy 、 split 、 take 、 takeRight 、 template 、 trim 、 trimEnd 、 trimStart和words
论据:
@param (array | object) $collection 要迭代的集合。
@param (callable | string | array) $iteratee 每次迭代调用的函数。
返回:
@return array 返回新的映射数组。
例子:
<?php
use function _ map ;
function square ( int $ n ) {
return $ n * $ n ;
}
map ([ 4 , 8 ], $ square );
// => [16, 64]
map (( object ) [ ' a ' => 4 , ' b ' => 8 ], $ square );
// => [16, 64] (iteration order is not guaranteed)
$ users = [
[ ' user ' => ' barney ' ],
[ ' user ' => ' fred ' ]
];
// The `property` iteratee shorthand.
map ( $ users , ' user ' );
// => ['barney', 'fred']此方法类似于sortBy ,只不过它允许指定要排序的迭代器的排序顺序。如果未指定orders ,则所有值均按升序排序。否则,指定“desc”顺序(对应值的降序)或“asc”(升序)顺序。
论据:
@param (iterable | null) $collection 要迭代的集合。
@param (array[] | callable[] | string[]) $iteratee 要排序的 iteratee。
@param string[] $orders iteratees的排序顺序。
返回:
@return array 新的排序数组。
例子:
<?php
use function _ orderBy ;
$ users = [
[ ' user ' => ' fred ' , ' age ' => 48 ],
[ ' user ' => ' barney ' , ' age ' => 34 ],
[ ' user ' => ' fred ' , ' age ' => 40 ],
[ ' user ' => ' barney ' , ' age ' => 36 ]
]
// Sort by `user` in ascending order and by `age` in descending order.
orderBy ( $ users , [ ' user ' , ' age ' ], [ ' asc ' , ' desc ' ])
// => [['user' => 'barney', 'age' => 36], ['user' => 'barney', 'age' => 34], ['user' => 'fred', 'age' => 48], ['user' => 'fred', 'age' => 40]]创建一个分成两组的元素数组,第一组包含predicate返回 true 的元素,第二组包含predicate返回 falsey 的元素。该谓词通过一个参数调用:(值)。
论据:
@param iterable $collection 要迭代的集合。
@param callable $predicate 每次迭代调用的函数。
返回:
@return array 分组元素的数组。
例子:
<?php
use function _ partition ;
$ users = [
[ ' user ' => ' barney ' , ' age ' => 36 , ' active ' => false ],
[ ' user ' => ' fred ' , ' age ' => 40 , ' active ' => true ],
[ ' user ' => ' pebbles ' , ' age ' => 1 , ' active ' => false ]
];
partition ( $ users , function ( $ user ) { return $ user [ ' active ' ]; })
// => objects for [['fred'], ['barney', 'pebbles']]将collection减少到一个值,该值是通过iteratee运行collection中每个元素的累积结果,其中每个连续调用都提供前一个调用的返回值。如果未给出accumulator ,则使用collection的第一个元素作为初始值。使用四个参数调用迭代器:(累加器、值、索引|键、集合)。
许多 lodash 方法都被保护为像reduce 、 reduceRight和transform这样的方法的迭代器。
受保护的方法有: assign 、 defaults 、 defaultsDeep 、 includes 、 merge 、 orderBy和sortBy
论据:
@param iterable $collection 要迭代的集合。
@param mix $iteratee 每次迭代调用的函数。
@param mix $accumulator 初始值。
返回:
@return mix 返回累加值。
例子:
<?php
use function _ reduce ;
reduce ([ 1 , 2 ], function ( $ sum , $ n ) { return $ sum + $ n ; }, 0 )
// => 3
reduce ([ ' a ' => 1 , ' b ' => 2 , ' c ' => 1 ], function ( $ result , $ value , $ key ) {
if (! isset ( $ result [ $ value ])) {
$ result [ $ value ] = [];
}
$ result [ $ value ][] = $ key ;
return $ result ;
}, [])
// => ['1' => ['a', 'c'], '2' => ['b']] (iteration order is not guaranteed)此方法类似于reduce ,只不过它从右到左迭代collection的元素。
论据:
@param iterable $collection 要迭代的集合。
@param mix $iteratee 每次迭代调用的函数。
@param mix $accumulator 初始值。
返回:
@return mix 返回累加值。
例子:
<?php
use function _ reduceRight ;
$ array = [[ 0 , 1 ], [ 2 , 3 ], [ 4 , 5 ]];
reduceRight ( array , (flattened, other) => flattened. concat (other), [])
// => [4, 5, 2, 3, 0, 1]与filter相反,此方法返回predicate未返回 true 的collection元素。
论据:
@param iterable $collection 要迭代的集合。
@param callable $predicate 每次迭代调用的函数。
返回:
@return array 新的过滤数组。
例子:
<?php
use function _ reject ;
$ users = [
[ ' user ' => ' barney ' , ' active ' => true ],
[ ' user ' => ' fred ' , ' active ' => false ]
]
reject ( $ users , ' active ' )
// => objects for ['fred']通过返回数组值的长度或对象的公共属性的数量来获取collection的大小。
论据:
@param (array | object | string) $collection 要检查的集合。
返回:
@return int 返回集合大小。
例子:
<?php
use function _ size ;
size ([ 1 , 2 , 3 ]);
// => 3
size ( new class { public $ a = 1 ; public $ b = 2 ; private $ c = 3 ; });
// => 2
size ( ' pebbles ' );
// => 7检查predicate是否为collection的任何元素返回 true 。一旦predicate返回真值,迭代就会停止。使用三个参数调用谓词:(值、索引|键、集合)。
论据:
@param iterable $collection 要迭代的集合。
@param (callable | string | array) $predicate 每次迭代调用的函数。
返回:
@return boolean 如果任何元素通过谓词检查,则返回true ,否则返回false 。
例子:
<?php
use function _ some ;
some ([ null , 0 , ' yes ' , false ], , function ( $ value ) { return is_bool ( $ value ); }));
// => true
$ users = [
[ ' user ' => ' barney ' , ' active ' => true ],
[ ' user ' => ' fred ' , ' active ' => false ]
];
// The `matches` iteratee shorthand.
some ( $ users , [ ' user ' => ' barney ' , ' active ' => false ]);
// => false
// The `matchesProperty` iteratee shorthand.
some ( $ users , [ ' active ' , false ]);
// => true
// The `property` iteratee shorthand.
some ( $ users , ' active ' );
// => true创建一个元素数组,按通过每个迭代器运行集合中每个元素的结果按升序排序。此方法执行稳定排序,即保留相等元素的原始排序顺序。迭代器通过一个参数调用:(值)。
论据:
@param (array | object | null) $collection 要迭代的集合。
@param (callable | callable[]) $iteratees 要排序的 iteratees。
返回:
@return array 返回新的排序数组。
例子:
<?php
use function _ sortBy ;
$ users = [
[ ' user ' => ' fred ' , ' age ' => 48 ],
[ ' user ' => ' barney ' , ' age ' => 36 ],
[ ' user ' => ' fred ' , ' age ' => 40 ],
[ ' user ' => ' barney ' , ' age ' => 34 ],
];
sortBy ( $ users , [ function ( $ o ) { return $ o [ ' user ' ]; }]);
// => [['user' => 'barney', 'age' => 36], ['user' => 'barney', 'age' => 34], ['user' => 'fred', 'age' => 48], ['user' => 'fred', 'age' => 40]]
sortBy ( $ users , [ ' user ' , ' age ' ]);
// => [['user' => 'barney', 'age' => 34], ['user' => 'barney', 'age' => 36], ['user' => 'fred', 'age' => 40], ['user' => 'fred', 'age' => 48]] 获取自 Unix 纪元(1970 年 1 月 1 日 00:00:00 UTC)以来经过的毫秒数的时间戳。
论据:
返回:
@return int 返回时间戳。
例子:
<?php
use function _ now ;
now ();
// => 1511180325735 与before相反;此方法创建一个函数,一旦调用func n或多次,该函数就会调用该函数。
论据:
@param int $n 调用func之前的调用次数。
@param Callable $func 要限制的函数。
返回:
@return Callable 返回新的受限函数。
例子:
<?php
use function _ after ;
$ saves = [ ' profile ' , ' settings ' ];
$ done = after ( count ( $ saves ), function () {
echo ' done saving! ' ;
});
forEach ( $ saves , function ( $ type ) use ( $ done ) {
asyncSave ([ ' type ' => $ type , ' complete ' => $ done ]);
});
// => Prints 'done saving!' after the two async saves have completed.创建一个调用func的函数,最多带有n参数,忽略任何其他参数。
论据:
@param callable $func 限制参数的函数。
@param int $n 数量上限。
返回:
@return Callable 返回新的上限函数。
例子:
<?php
use function _ ary ;
map ([ ' 6 ' , ' 8 ' , ' 10 ' ], ary ( ' intval ' , 1 ));
// => [6, 8, 10]创建一个使用所创建函数的参数调用func的函数,同时调用次数少于n次。对创建的函数的后续调用将返回上次func调用的结果。
论据:
@param int $n 不再调用func调用次数。
@param callable $func 要限制的函数。
返回:
@return callable 返回新的受限函数。
例子:
<?php
use function _ before ;
$ users = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ];
$ result = uniqBy ( map ( $ users , before ( 5 , [ $ repository , ' find ' ])), ' id ' )
// => Fetch up to 4 results.创建一个函数,该函数使用object的this绑定以及附加到它接收的参数前面partials来调用func 。
论据:
@param callable $function 要绑定的函数。
@param (object | mix) $object func的object绑定。
@param array<int, mix> $partials 要部分应用的参数。
返回:
@return callable 返回新的绑定函数。
例子:
<?php
use function _ bind ;
function greet ( $ greeting , $ punctuation ) {
return $ greeting . ' ' . $ this -> user . $ punctuation ;
}
$ object = $ object = new class {
public $ user = ' fred ' ;
};
$ bound = bind ( ' greet ' , $ object , ' hi ' );
$ bound ( ' ! ' );
// => 'hi fred!'创建一个函数,该函数调用$object的$function方法,并将$partials附加到它接收的参数前面。
此方法与bind不同,它允许绑定函数引用可能被重新定义或尚不存在的方法
论据:
@param object $object 调用方法的对象。
@param string $function 方法的名称。
@param array<int, mix> $partials 要部分应用的参数。
返回:
@return callable 返回新的绑定函数。
例子:
<?php
use function _ bindKey ;
$ object = new class {
private $ user = ' fred ' ;
function greet ( $ greeting , $ punctuation ) {
return $ greeting . ' ' . $ this -> user . $ punctuation ;
}
};
$ bound = bindKey ( $ object , ' greet ' , ' hi ' );
$ bound ( ' ! ' );
// => 'hi fred!'创建一个接受func参数的函数,如果至少提供了arity个参数,则调用func返回其结果,或者返回接受剩余func参数的函数,依此类推。如果func.length不够,可以指定func的数量。
_.curry.placeholder值(在整体构建中默认为_ )可以用作所提供参数的占位符。
注意:此方法不会设置柯里化函数的“length”属性。
论据:
@param callable $func 要柯里化的函数。
@param (int | null) $arity func的数量。
返回:
@return callable 返回新的柯里化函数。
例子:
<?php
use function _ curry ;
$ abc = function ( $ a , $ b , $ c ) {
return [ $ a , $ b , $ c ];
};
$ curried = curry ( $ abc );
$ curried ( 1 )( 2 )( 3 );
// => [1, 2, 3]
$ curried ( 1 , 2 )( 3 );
// => [1, 2, 3]
$ curried ( 1 , 2 , 3 );
// => [1, 2, 3]
// Curried with placeholders.
$ curried ( 1 )(_, 3 )( 2 );
// => [1, 2, 3]wait毫秒后调用func 。调用 func 时,将向func提供任何其他参数。
论据:
@param callable $func 要延迟的函数。
@param int $wait 延迟调用的毫秒数。
@param array<int, 混合> $args
返回:
@return int 计时器 ID。
例子:
<?php
use function _ delay ;
delay ( function ( $ text ) {
echo $ text ;
}, 1000 , ' later ' );
// => Echo 'later' after one second.创建一个调用func并反转参数的函数。
论据:
@param callable $func 翻转参数的函数。
返回:
@return callable 返回新的翻转函数。
例子:
<?php
use function _ flip ;
$ flipped = flip ( function () {
return func_get_args ();
});
flipped ( ' a ' , ' b ' , ' c ' , ' d ' );
// => ['d', 'c', 'b', 'a']创建一个存储func结果的函数。如果提供了resolver ,它会根据提供给记忆函数的参数确定用于存储结果的缓存键。默认情况下,提供给记忆函数的第一个参数用作地图缓存键
注意:缓存作为存储函数的cache属性公开。可以通过将_.memoize.Cache构造函数替换为实例实现clear 、 delete 、 get 、 has和set的Map方法接口的构造函数来自定义其创建。
论据:
@param callable $func 存储其输出的函数。
@param (callable | null) $resolver 解析缓存键的函数。
返回:
@return callable 返回新的记忆函数。
例子:
<?php
use function _ memoize ;
$ object = [ ' a ' => 1 , ' b ' => 2 ];
$ other = [ ' c ' => 3 , ' d ' => 4 ];
$ values = memoize ( ' _values ' );
$ values ( $ object );
// => [1, 2]
$ values ( $ other );
// => [3, 4]
$ object [ ' a ' ] = 2 ;
$ values ( $ object );
// => [1, 2]
// Modify the result cache.
$ values -> cache -> set ( $ object , [ ' a ' , ' b ' ]);
$ values ( $ object );
// => ['a', 'b']创建一个否定谓词func结果的函数
论据:
@param callable $predicate 要否定的谓词。
返回:
@return callable 返回新的否定函数。
例子:
<?php
use function _ negate ;
function isEven ( $ n ) {
return $ n % 2 == 0 ;
}
filter ([ 1 , 2 , 3 , 4 , 5 , 6 ], negate ( $ isEven ));
// => [1, 3, 5]创建一个仅限调用func一次的函数。重复调用该函数会返回第一次调用的值。使用所创建函数的参数调用func 。
论据:
@param callable $func 要限制的函数。
返回:
@return 可调用新的受限函数。
例子:
<?php
use function _ once ;
$ initialize = once ( ' createApplication ' );
$ initialize ();
$ initialize ();
// => `createApplication` is invoked once创建一个调用func并转换其参数的函数。
论据:
@param callable $func 要包装的函数。
@param callable[] $transforms 参数转换。
返回:
@return 可调用新函数。
例子:
<?php
use function _ overArgs ;
function doubled ( $ n ) {
return $ n * 2 ;
}
function square ( $ n ) {
return $ n * $ n ;
}
$ func = overArgs ( function ( $ x , $ y ) {
return [ $ x , $ y ];
}, [ ' square ' , ' doubled ' ]);
$ func ( 9 , 3 );
// => [81, 6]
$ func ( 10 , 5 );
// => [100, 10]创建一个调用func的函数,并将partials添加到它接收的参数中。
论据:
@param callable $func 部分应用参数的函数。
@param array<int, mix> $partials 要部分应用的参数。
返回:
@return callable 返回新的部分应用函数。
例子:
<?php
use function _ partial ;
function greet ( $ greeting , $ name ) {
return $ greeting . ' ' . $ name ;
}
$ sayHelloTo = partial ( ' greet ' , ' hello ' );
$ sayHelloTo ( ' fred ' );
// => 'hello fred'创建一个函数,该函数使用创建的函数的this绑定以及以数组形式提供的从start到结束的参数来调用func 。
论据:
@param callable $func 应用剩余参数的函数。
@param (int | null) $start 剩余参数的开始位置。
返回:
@return callable 返回新函数。
例子:
<?php
use function _ rest ;
$ say = rest ( function ( $ what , $ names ) {
return $ what . ' ' . implode ( ' , ' , initial ( $ names )) .
( size ( $ names ) > 1 ? ' , & ' : '' ) . last ( $ names );
});
$ say ( ' hello ' , ' fred ' , ' barney ' , ' pebbles ' );
// => 'hello fred, barney, & pebbles'创建一个函数,该函数使用 create 函数的this绑定和一个参数数组来调用func ,就像Function#apply一样。
注意:该方法基于展开运算符。
论据:
@param callable $func 用于传播参数的函数。
@param int $start 展开的起始位置。
返回:
@return callable 返回新函数。
例子:
<?php
use function _ spread ;
$ say = spread ( function ( $ who , $ what ) {
return $ who . ' says ' . $ what ;
});
$ say ([ ' fred ' , ' hello ' ]);
// => 'fred says hello'创建一个最多接受一个参数的函数,忽略任何其他参数。
论据:
@param callable $func 限制参数的函数。
返回:
@return 可调用新的上限函数。
例子:
<?php
use function _ unary ;
map ([ ' 6 ' , ' 8 ' , ' 10 ' ], unary ( ' intval ' ));
// => [6, 8, 10]创建一个函数,为wrapper提供value作为其第一个参数。提供给函数的任何其他参数都将附加到提供给wrapper参数中。
论据:
@param mix $value 要包装的值。
@param callable $wrapper 包装函数。
返回:
@return 可调用新函数。
例子:
<?php
use function _ wrap ;
$ p = wrap ( ' _escape ' , function ( $ func , $ text ) {
return ' <p> ' . $ func ( $ text ) . ' </p> ' ;
});
$ p ( ' fred, barney, & pebbles ' );
// => '<p>fred, barney, & pebbles</p>' 对两个值进行比较以确定它们是否相等。
论据:
@param mix $value 要比较的值。
@param mix $other 要比较的其他值。
返回:
@return boolean如果值等效,则返回true ,否则false 。
例子:
<?php
use function _ eq ;
$ object = ( object ) [ ' a ' => 1 ];
$ other = ( object ) [ ' a ' => 1 ];
eq ( $ object , $ object );
// => true
eq ( $ object , $ other );
// => false
eq ( ' a ' , ' a ' );
// => true
eq ([ ' a ' ], ( object ) [ ' a ' ]);
// => false
eq ( INF , INF );
// => true在两个值之间进行深入比较,以确定它们是否等效。
注意:此方法支持比较数组,布尔值,日期对象,异常对象,splobjectStorage,数字,字符串,键入数组,资源,DOM节点。对象由其自己,而不是继承,枚举的属性进行比较。
论据:
@Param混合$值比较值。
@Param混合$其他值得比较的值。
返回:
@return bool如果值等效,则返回true ,否则false 。
例子:
<?php
use function _ isEqual ;
$ object = [ ' a ' => 1 ]
$ other = [ ' a ' => ' 1 ' ]
isEqual ( $ object , $ other )
// => true
$ object === $ other
// => false检查value是否为Exception , ParseError , error , Throwable , soapfault , DOMException , pdoexception`,object。
论据:
@param混合$值检查值。
返回:
@return boolean如果value是错误对象,则返回true ,否则false 。
例子:
<?php
use function _ isError ;
isError ( new Exception ())
// => true
isError (Exception::Class)
// => false 添加两个数字。
论据:
@param(int | float | string)$在加法中为第一个数字增值。
@param(int | float | String)$加法中的第二个数字。
返回:
@return(int | float)返回总数。
例子:
<?php
use function _ add ;
add ( 6 , 4 );
// => 10计算array的最大值。如果array为空还是虚假,则返回空。
论据:
@param(数组| null)$阵列将数组到迭代。
返回:
@return(int | null)返回最大值。
例子:
<?php
use function _ max ;
max ([ 4 , 2 , 8 , 6 ]);
// => 8
max ([]);
// => null此方法就像max一样,除了它接受array中每个元素的iteratee以生成值排名的标准。 ITEMERE被一个参数调用:(value)。
论据:
@param数组$阵列to to Toterate Over。
@param(callable | string)$ iterateE每个元素调用。
返回:
@return混合返回最大值。
例子:
<?php
use function _ maxBy ;
$ objects = [[ ' n ' => 1 ], [ ' n ' => 2 ]];
maxBy ( $ objects , function ( $ o ) { return $ o [ ' n ' ]; });
// => ['n' => 2]
// The `property` iteratee shorthand.
maxBy ( $ objects , ' n ' );
// => ['n' => 2] number在包含lower和upper内。
论据:
@param int $编号夹具的号码。
@param int $降低下限。
@param int $上限上限。
返回:
@return int返回夹具的数字。
例子:
<?php
use function _ clamp ;
clamp (- 10 , - 5 , 5 )
// => -5
clamp ( 10 , - 5 , 5 )
// => 5检查number是否在start和end (但不包括)之间。如果未指定end ,则将其设置为“ start start然后设置为0 。如果start大于end则将参数交换以支持负范围。
论据:
@param float $编号要检查的号码。
@param float $开始范围的开始。
@param float $结束范围的结束。
返回:
@return boolean如果number在范围内,则返回true ,否则false 。
例子:
<?php
use function _ inRange ;
inRange ( 3 , 2 , 4 )
// => true
inRange ( 4 , 8 )
// => true
inRange ( 4 , 2 )
// => false
inRange ( 2 , 2 )
// => false
inRange ( 1.2 , 2 )
// => true
inRange ( 5.2 , 4 )
// => false
inRange (- 3 , - 2 , - 6 )
// => true在包含的lower和upper之间产生一个随机数。如果仅提供一个参数,则返回给定0之间的数字。如果floating是true ,或者是lower ,则浮点数为upper ,而不是整数。
论据:
@param(int | float | bool)$降低下限。
@param(int | float | bool)$上限上限。
@param(bool | null)$浮动指定返回浮点数。
返回:
@return(int | float)返回随机数。
例子:
<?php
use function _ random ;
random ( 0 , 5 )
// => an integer between 0 and 5
random ( 5 )
// => also an integer between 0 and 5
random ( 5 , true )
// => a floating-point number between 0 and 5
random ( 1.2 , 5.2 )
// => a floating-point number between 1.2 and 5.2 获取对象路径处的值。如果解析值为null,则将默认值返回其位置。
论据:
@Param混合$对象缔合数组或对象从
@param(array | string)$路径点分离或字符串的数组
@param混合$ defaultValue(可选)未解决或无空值返回的值。
返回:
@return混合返回解决值。
例子:
<?php
use function _ get ;
$ sampleArray = [ " key1 " => [ " key2 " => [ " key3 " => " val1 " , " key4 " => "" ]]];
get ( $ sampleArray , ' key1.key2.key3 ' );
// => "val1"
get ( $ sampleArray , ' key1.key2.key5 ' , " default " );
// => "default"
get ( $ sampleArray , ' key1.key2.key4 ' , " default " );
// => ""创建一个由采摘object属性组成的对象。
论据:
@param对象$对象源对象。
@param(String | String [])$路径属于选择的属性路径。
返回:
@return stdclass返回新对象。
例子:
<?php
use function _ pick ;
$ object = ( object ) [ ' a ' => 1 , ' b ' => ' 2 ' , ' c ' => 3 ];
pick ( $ object , [ ' a ' , ' c ' ]);
// => (object) ['a' => 1, 'c' => 3]创建一个由object属性组成的对象predicate返回真相。谓词带有两个参数:(value,key)。
论据:
@param(object | null)$对象源对象。
@param callable $谓词每个属性调用的函数。
返回:
@return stdclass返回新对象。
例子:
<?php
use function _ pickBy ;
$ object = ( object ) [ ' a ' => 1 , ' b ' => ' abc ' , ' c ' => 3 ];
pickBy (object, ' is_numeric ' );
// => (object) ['a' => 1, 'c' => 3] 创建一个lodash包装器实例,该实例将启用的显式方法链序列包装value 。此类序列的结果必须用->value()解开。
论据:
@Param混合$值包装的值。
返回:
@return _返回新的lodash包装器实例。
例子:
<?php
use function _ chain ;
$ users = [
[ ' user ' => ' barney ' , ' age ' => 36 ],
[ ' user ' => ' fred ' , ' age ' => 40 ],
[ ' user ' => ' pebbles ' , ' age ' => 1 ],
];
$ youngest = chain ( $ users )
-> sortBy ( ' age ' )
-> map ( function ( $ o ) {
return $ o [ ' user ' ] . ' is ' . $ o [ ' age ' ];
})
-> head ()
-> value ();
// => 'pebbles is 1' 将string转换为骆驼盒。
论据:
@param字符串$字符串转换字符串。
返回:
@return String返回骆驼壳字符串。
例子:
<?php
use function _ camelCase ;
camelCase ( ' Foo Bar ' )
// => 'fooBar'
camelCase ( ' --foo-bar-- ' )
// => 'fooBar'
camelCase ( ' __FOO_BAR__ ' )
// => 'fooBar'将string的第一个字符转换为上情况,然后将其剩余字符转换为较低的情况。
论据:
@param字符串$字符串字符串大写。
返回:
@return字符串返回大写字符串。
例子:
<?php
use function _ capitalize ;
capitalize ( ' FRED ' )
// => 'Fred'通过转换[LATIN-1补充string (https => // en.wikipedia.org/wiki/wiki/latin-1_supplement_(unicode_block)#character_table_table)和[latin extended-a](https =>/https =>/en.wikipedia。 org/wiki/latin_extended-a)基本拉丁字母的信件并删除[结合音位标记](https) => // en.wikipedia.org/wiki/combining_diacritical_marks)。
论据:
@param字符串$字符串将字符串转换为deburr。
返回:
@return字符串返回Deburred String。
例子:
<?php
use function _ deburr ;
deburr ( ' déjà vu ' )
// => 'deja vu'检查string是否以给定目标字符串结束。
论据:
@param字符串$字符串的字符串要检查。
@param字符串$目标字符串要搜索。
@param int $定位要搜索的位置。
返回:
@return boolean如果string以target结束,则返回true ,else false 。
例子:
<?php
use function _ endsWith ;
endsWith ( ' abc ' , ' c ' )
// => true
endsWith ( ' abc ' , ' b ' )
// => false
endsWith ( ' abc ' , ' b ' , 2 )
// => true将string的字符“&”,“”,“','”和“”转换为其相应的HTML实体。
尽管“>”字符被逃脱了对称性,但诸如“>”和“/“”的字符不需要在HTML中逃脱,除非它们是标签或未引用的属性值的一部分,否则没有特殊的含义。有关更多详细信息,请参见Mathias Bynens的文章(在“半半有趣的事实”下)。
使用HTML时,您应始终引用属性值以减少XSS向量。
论据:
@param字符串$字符串逃脱。
返回:
@return字符串返回逃脱的字符串。
例子:
<?php
use function _ escape ;
escape ( ' fred, barney, & pebbles ' )
// => 'fred, barney, & pebbles'逃脱RegExp特殊字符“^”,“ $”,“”,“。”,“*”,“+”,“?”,“”,“,”,“),”,“ [,“”],”,“ { “,”,“}”和“ |”在string中。
论据:
@param字符串$字符串逃脱。
返回:
@return字符串返回逃脱的字符串。
例子:
<?php
use function _ escapeRegExp ;
escapeRegExp ( ' [lodash](https://lodash.com/) ' )
// => '[lodash](https://lodash.com/)'将string转换为烤肉串。
论据:
@param字符串$字符串转换字符串。
返回:
@return字符串返回烤肉串壳字符串。
例子:
<?php
use function _ kebabCase ;
kebabCase ( ' Foo Bar ' )
// => 'foo-bar'
kebabCase ( ' fooBar ' )
// => 'foo-bar'
kebabCase ( ' __FOO_BAR__ ' )
// => 'foo-bar'将string作为空间分开的单词转换为较低的情况。
论据:
@param字符串$字符串转换字符串。
返回:
@return字符串返回下壳字符串。
例子:
<?php
use function _ lowerCase ;
lowerCase ( ' --Foo-Bar-- ' )
// => 'foo bar'
lowerCase ( ' fooBar ' )
// => 'foo bar'
lowerCase ( ' __FOO_BAR__ ' )
// => 'foo bar'将string的第一个字符转换为较低的情况。
论据:
@param字符串$字符串转换字符串。
返回:
@return字符串返回转换的字符串。
例子:
<?php
use function _ lowerFirst ;
lowerFirst ( ' Fred ' )
// => 'fred'
lowerFirst ( ' FRED ' )
// => 'fRED'如果比length短,则在左侧和右侧的垫子string 。如果不能将填充字符截断,如果不能将其均匀划分为length 。
论据:
@param字符串$字符串将字符串到垫子。
@param int $长度填充长度。
@param字符串$ chars用作填充的字符串。
返回:
@return字符串返回填充字符串。
例子:
<?php
use function _ pad ;
pad ( ' abc ' , 8 )
// => ' abc '
pad ( ' abc ' , 8 , ' _- ' )
// => '_-abc_-_'
pad ( ' abc ' , 2 )
// => 'abc'右侧的垫子string在右侧,如果length短。如果填充字符超过length则将其截断。
论据:
@param字符串$字符串将字符串到垫子。
@param int $长度填充长度。
@param字符串$ chars用作填充的字符串。
返回:
@return字符串返回填充字符串。
例子:
<?php
use function _ padEnd ;
padEnd ( ' abc ' , 6 )
// => 'abc '
padEnd ( ' abc ' , 6 , ' _- ' )
// => 'abc_-_'
padEnd ( ' abc ' , 2 )
// => 'abc'如果比length短,则在左侧的垫子string 。如果填充字符超过length则将其截断。
s论点:
@param String $ string =''垫的字符串。
@param int $长度填充长度。
@param字符串$ chars用作填充的字符串。
返回:
@return字符串返回填充字符串。
例子:
<?php
use function _ padStart ;
padStart ( ' abc ' , 6 )
// => ' abc'
padStart ( ' abc ' , 6 , ' _- ' )
// => '_-_abc'
padStart ( ' abc ' , 2 )
// => 'abc'将string转换为指定radix的整数。如果radix undefined或0 ,则使用10的radix ,除非string是十六进制,在这种情况下,使用16的radix 。
注意:此方法使用PHP的内置整数铸造,这不一定与parseInt的ES5实现保持一致。
论据:
@param(int | float | string)$字符串转换。
@param int $ radix radix解释string 。
返回:
@return int返回转换后的整数。
例子:
<?php
use function _ parseInt ;
parseInt ( ' 08 ' )
// => 8重复给定的字符串n时间。
论据:
@param字符串$字符串重复的字符串。
@param int $ n重复字符串的次数。
返回:
@return字符串返回重复的字符串。
例子:
<?php
use function _ repeat ;
repeat ( ' * ' , 3 )
// => '***'
repeat ( ' abc ' , 2 )
// => 'abcabc'
repeat ( ' abc ' , 0 )
// => ''用replacement在pattern string替换匹配项。
注意:此方法基于String#replace 。
论据:
@param字符串$字符串的字符串要修改。
@param字符串$模式替换的模式。
@param(callable | string)$替换匹配替换。
返回:
@return字符串返回修改后的字符串。
例子:
<?php
use function _ replace ;
replace ( ' Hi Fred ' , ' Fred ' , ' Barney ' )
// => 'Hi Barney'将string转换为蛇案。
论据:
@param字符串$字符串转换字符串。
返回:
@return String返回蛇壳字符串。
例子:
<?php
use function _ snakeCase ;
snakeCase ( ' Foo Bar ' )
// => 'foo_bar'
snakeCase ( ' fooBar ' )
// => 'foo_bar'
snakeCase ( ' --FOO-BAR-- ' )
// => 'foo_bar'通过separator拆分string 。
注意:此方法基于String#split 。
论据:
@param字符串$字符串拆分字符串。
@param字符串$分隔符分离器模式要分开。
@param int $将长度限制为截断结果。
返回:
@return数组返回字符串段。
例子:
<?php
use function _ split ;
split ( ' a-b-c ' , ' - ' , 2 )
// => ['a', 'b']将string转换为启动情况。
论据:
@param字符串$字符串转换字符串。
返回:
@return字符串返回开始cased字符串。
例子:
<?php
use function _ startCase ;
startCase ( ' --foo-bar-- ' )
// => 'Foo Bar'
startCase ( ' fooBar ' )
// => 'Foo Bar'
startCase ( ' __FOO_BAR__ ' )
// => 'FOO BAR'检查string是否从给定的目标字符串开始。
论据:
@param字符串$字符串的字符串要检查。
@param字符串$目标字符串要搜索。
@param int $位置要搜索的位置。
返回:
@return boolean如果string以target开头,则返回true ,else false 。
例子:
<?php
use function _ startsWith ;
startsWith ( ' abc ' , ' a ' )
// => true
startsWith ( ' abc ' , ' b ' )
// => false
startsWith ( ' abc ' , ' b ' , 1 )
// => true创建一个编译的模板函数,该功能可以在“ Escape”定界符中的“插值”定界符,HTML-Escape插值数据属性中插值数据属性,并在“评估”定界符中执行PHP。数据属性可以作为模板中的自由变量访问。如果给出设置对象,则需要优先于$templateSettings值。
REGEXP $ options ['easpe'] = _ :: $ spemplatesettings ['easce'] html“逃生”定界符。 Regexp $ options ['evaluate'] = _ :: $ spemplatesettings ['evaluate']“评估”定界符。数组$ options ['imports'] = _ :: $ templatesettings ['imports']作为自由变量导入到模板中的对象。 REGEXP $ options ['interpaly'] = _ :: $ spemplatesettings ['interpaly']“插螺旋”定界符。
论据:
@param字符串$字符串模板字符串。
@Param数组$选项选项阵列。
返回:
@return callable返回编译模板功能。
例子:
<?php
use function _ template ;
// Use the "interpolate" delimiter to create a compiled template.
$ compiled = template ( ' hello <%= user %>! ' )
$ compiled ([ ' user ' => ' fred ' ])
// => 'hello fred!'
// Use the HTML "escape" delimiter to escape data property values.
$ compiled = template ( ' <b><%- value %></b> ' )
$ compiled ([ ' value ' => ' <script> ' ])
// => '<b><script></b>'
// Use the "evaluate" delimiter to execute JavaScript and generate HTML.
$ compiled = template ( ' <% foreach($users as $user) { %><li><%- user %></li><% }%> ' )
$ compiled ([ ' users ' => [ ' fred ' , ' barney ' ] ])
// => '<li>fred</li><li>barney</li>'
// Use the internal `print` function in "evaluate" delimiters.
$ compiled = template ( ' <% print("hello " + $user)%>! ' )
$ compiled ([ ' user ' => ' barney ' ])
// => 'hello barney!'
// Use backslashes to treat delimiters as plain text.
$ compiled = template ( ' <%= " \ <%- value % \ >" %> ' )
$ compiled ([ ' value ' => ' ignored ' ])
// => '<%- value %>'
// Use the `imports` option to import functions or classes with aliases.
$ text = ' <% all($users, function($user) { %><li><%- user %></li><% })%> '
$ compiled = template( $ text , { ' imports ' : { ' _each ' : ' all ' } })
$ compiled ([ ' users ' => [ ' fred ' , ' barney ' ] ])
// => '<li>fred</li><li>barney</li>'
// Use custom template delimiters.
_:: $ templateSettings [ ' interpolate ' ] = ' {{([sS]+?)}} '
$ compiled = template ( ' hello {{ user }}! ' )
$ compiled ([ ' user ' => ' mustache ' ])
// => 'hello mustache!'
// Use the `source` property to access the compiled source of the template
template ( $ mainText )-> source ;总体上将string转换为较低的情况
论据:
@param字符串$字符串转换字符串。
返回:
@return字符串返回下壳字符串。
例子:
<?php
use function _ toLower ;
toLower ( ' --Foo-Bar-- ' )
// => '--foo-bar--'
toLower ( ' fooBar ' )
// => 'foobar'
toLower ( ' __FOO_BAR__ ' )
// => '__foo_bar__'整体上将string转换为上情况
论据:
@param字符串$字符串转换字符串。
返回:
@return字符串返回上层壳字符串。
例子:
<?php
use function _ toUpper ;
toUpper ( ' --foo-bar-- ' )
// => '--FOO-BAR--'
toUpper ( ' fooBar ' )
// => 'FOOBAR'
toUpper ( ' __foo_bar__ ' )
// => '__FOO_BAR__'从string中删除领先和尾随的空格或指定字符。
论据:
@param字符串$字符串将字符串到修剪。
@param字符串$ char将字符符为修剪。
返回:
@return字符串返回修剪的字符串。
例子:
<?php
use function _ trim ;
trim ( ' abc ' )
// => 'abc'
trim ( ' -_-abc-_- ' , ' _- ' )
// => 'abc'从string中删除尾随的空格或指定字符。
论据:
@param字符串$字符串将字符串到修剪。
@param字符串$ char将字符符为修剪。
返回:
@return字符串返回修剪的字符串。
例子:
<?php
use function _ trimEnd ;
trimEnd ( ' abc ' )
// => ' abc'
trimEnd ( ' -_-abc-_- ' , ' _- ' )
// => '-_-abc'从string中删除领先的空格或指定字符。
论据:
@param字符串$字符串将字符串到修剪。
@param字符串$ char将字符符为修剪。
返回:
@return字符串返回修剪的字符串。
例子:
<?php
use function _ trimStart ;
trimStart ( ' abc ' )
// => 'abc '
trimStart ( ' -_-abc-_- ' , ' _- ' )
// => 'abc-_-'如果字符串比给定的最大字符串长度长,则截断string 。截断的字符串的最后一个字符被默认为“ ...”的遗漏字符串替换。
长度= 30最大字符串长度。遗漏='...'省略文本的字符串。分离器分离器图案要截断。
论据:
@param字符串$字符串将字符串截断。
@param数组$选项选项对象。
返回:
@return字符串返回截断的字符串。
例子:
<?php
use function _ truncate ;
truncate ( ' hi-diddly-ho there, neighborino ' )
// => 'hi-diddly-ho there, neighbo...'
truncate ( ' hi-diddly-ho there, neighborino ' , [
' length ' => 24 ,
' separator ' => ' '
])
// => 'hi-diddly-ho there,...'
truncate ( ' hi-diddly-ho there, neighborino ' , [
' length ' => 24 ,
' separator ' => ' /,? +/ '
])
// => 'hi-diddly-ho there...'
truncate ( ' hi-diddly-ho there, neighborino ' , [
' omission ' => ' [...] '
])
// => 'hi-diddly-ho there, neig [...]'escape的倒数此方法转换了HTML实体& , < , > , "和'在其相应字符的string中。
论据:
@param字符串$字符串将字符串到unescape。
返回:
@return字符串返回UneScaped String。
例子:
<?php
use function _ unescape ;
unescape ( ' fred, barney, & pebbles ' )
// => 'fred, barney, & pebbles'将string作为空间分开的单词转换为上情况。
论据:
@param字符串$字符串转换字符串。
返回:
@return字符串返回上层cased string.s
例子:
<?php
use function _ upperCase ;
upperCase ( ' --foo-bar ' )
// => 'FOO BAR'
upperCase ( ' fooBar ' )
// => 'FOO BAR'
upperCase ( ' __foo_bar__ ' )
// => 'FOO BAR'将string的第一个字符转换为上情况。
参数:
@param字符串$字符串转换字符串。
返回:
@return字符串返回转换的字符串。
例子:
<?php
use function _ upperFirst ;
upperFirst ( ' fred ' )
// => 'Fred'
upperFirst ( ' FRED ' )
// => 'FRED'将string分成多种单词。
参数:
@param字符串$字符串的字符串要检查。
@param字符串$模式匹配单词的模式。
返回:
@return数组返回string的单词。
例子:
<?php
use function _ words ;
words ( ' fred, barney, & pebbles ' )
// => ['fred', 'barney', 'pebbles']
words ( ' fred, barney, & pebbles ' , ' /[^, ]+/g ' )
// => ['fred', 'barney', '&', 'pebbles'] 尝试调用func ,返回结果或捕获的错误对象。调用func时,将提供任何其他参数。
s论点:
@param callable $ func尝试尝试。
@param数组<int,混合> $ arg arg and func的参数。
返回:
@return(混合| throwable)返回func结果或错误对象。
例子:
<?php
use function _ attempt ;
// Avoid throwing errors for invalid PDO data source.
$ elements = attempt ( function () {
new PDO ( null );
});
if ( isError ( $ elements )) {
$ elements = [];
}检查值以确定是否应返回其位置默认值。如果值为NAN或NULL,则返回默认值。
参数:
@Param混合$值任何值。
@param混合$ defaultValue值返回$ value是null或nan
返回:
@return混合返回value 。
例子:
<?php
use function _ defaultTo ;
$ a = null ;
defaultTo ( $ a , " default " );
// => "default"
$ a = " x " ;
defaultTo ( $ a , " default " );
// => "x"此方法返回其收到的第一个参数。
参数:
@Param混合$值任何值。
返回:
@return混合返回value 。
例子:
<?php
use function _ identity ;
$ object = [ ' a ' => 1 ];
identity ( $ object ) === $ object ;
// => true创建一个函数,该函数在给定对象的path处返回值。
参数:
@param(array | string)$路径要获得的属性。
返回:
@return callable返回新的访问函数。
例子:
<?php
use function _ property ;
$ objects = [
[ ' a ' => [ ' b ' => 2 ] ],
[ ' a ' => [ ' b ' => 1 ] ]
];
map ( $ objects , property ( ' a.b ' ));
// => [2, 1]
map ( sortBy ( $ objects , property ([ ' a ' , ' b ' ])), ' a.b ' );
// => [1, 2]