spatie/array-functions

一些实用的数组助手

资助包维护!
spatie

1.9.4 2021-04-03 18:04 UTC

This package is auto-updated.

Last update: 2024-09-21 18:02:50 UTC


README

一些实用的数组函数

Latest Version on Packagist Software License Build Status Quality Score SensioLabsInsight Total Downloads

本包提供了一些实用的数组函数。

Spatie 是一家位于比利时的安特卫普的网页设计公司。您可以在我们的网站上找到我们所有开源项目的概述 在这里

支持我们

我们投入了大量资源来创建 一流的开放源代码包。您可以通过 购买我们的付费产品之一 来支持我们。

我们非常感谢您从您的家乡给我们寄来明信片,注明您正在使用我们哪些包。您可以在我们的 联系我们页面 找到我们的地址。我们将把所有收到的明信片发布在我们的 虚拟明信片墙上

明信片软件

您可以使用这个包(它是 MIT 许可),但如果它进入了您的生产环境,您则需要向我们寄来一张来自您家乡的明信片,注明您正在使用我们哪些包。

我们的地址是:Spatie,Kruikstraat 22,2018 安特卫普,比利时。

最优秀的明信片将被发布在我们的网站上开源页面。

安装

您可以通过 composer 安装此包

composer require spatie/array-functions

用法

以下函数在 Spatie 命名空间中提供

array_rand_value

/**
 * Get a random value from an array.
 *
 * @param array $array
 * @param int $numReq  The amount of values to return
 * @return mixed
 */
function array_rand_value(array $array, $numReq = 1)

array_rand_weighted

/**
 * Get a random value from an array, with the ability to skew the results.
 * Example: array_rand_weighted(['foo' => 1, 'bar' => 2]) has a 66% chance of returning bar.
 * 
 * @param array $key
 * 
 * @return mixed
 */
function array_rand_weighted(array $array)

values_in_array

/**
 * Determine if all given needles are present in the haystack.
 *
 * @param array|string $needles
 * @param array $haystack
 *
 * @return bool
 */
function values_in_array($needles, array $haystack)

array_keys_exist

/**
 * Determine if all given needles are present in the haystack as array keys.
 * 
 * @param array|string $needles
 * @param array $haystack
 *
 * @return bool
 */
function array_keys_exist($needles, array $haystack)

array_split_filter

/**
 * Returns an array with two elements.
 * 
 * Iterates over each value in the array passing them to the callback function.
 * If the callback function returns true, the current value from array is returned in the first
 * element of result array. If not, it is return in the second element of result array.
 *
 * Array keys are preserved.
 *
 * @param array $array
 * @param callable $callback
 * @return array
 *
 */
function array_split_filter(array $array, callable $callback)

array_split

/**
 * Split an array in the given amount of pieces.
 *
 * @param array $array
 * @param int $numberOfPieces
 * @param bool $preserveKeys
 * @throws \InvalidArgumentException
 * @return array
 */
function array_split(array $array, $numberOfPieces = 2, $preserveKeys = false)

array_merge_values

/**
 * Returns an array with the unique values from all the given arrays
 *
 * @param \array[] $arrays
 * @return array
 */
function array_merge_values(array ... $arrays)

array_flatten

/**
 * Flatten an array of arrays. The `$levels` parameter specifies how deep you want to
 * recurse in the array. If `$levels` is -1, the function will recurse infinitely.
 *
 * @param array $array
 * @param int $levels
 *
 * @return array
 */
function array_flatten(array $array, $levels = -1)

变更日志

请参阅 变更日志 了解最近有哪些更改。

测试

您可以使用以下命令运行测试

vendor/bin/phpunit

贡献

请参阅 贡献指南 了解详细信息。

安全

如果您发现有关安全的错误,请通过电子邮件发送至 security@spatie.be,而不是使用问题跟踪器。

鸣谢

关于 Spatie

Spatie 是一家位于比利时的安特卫普的网页设计公司。您可以在我们的网站上找到我们所有开源项目的概述 在这里

许可证

MIT 许可证(MIT)。请参阅 许可证文件 了解更多信息。