snscripts/array-helper

框架无关的数组助手

1.0.0 2016-09-04 22:11 UTC

This package is auto-updated.

Last update: 2024-09-10 21:01:53 UTC


README

Author Source Code Latest Version Software License Build Status

简介

此数组助手是一个遵循 PSR-2 规范的框架无关助手,旨在帮助以目前内置 PHP 函数无法实现的方式操作数组。此外,还增加了“集合”风格的功能。

要求

Composer

Array Helper 需要:

  • "php": ">=5.4.0"

如果您希望以开发模式运行并运行测试,则还需要以下依赖项:

  • "phpunit/phpunit": "~4.0"
  • "squizlabs/php_codesniffer": "~2.0"

安装

Composer

最简单的安装方式是通过 composer。

composer require snscripts/array-helper 1.*

或者将以下内容添加到您的项目的 composer.json 文件中。

{
    "require": {
        "snscripts/array-helper": "1.*"
    }
}

设置

要设置以供使用,只需创建一个 ArrayHelper 实例并将您想要操作的数组作为构造函数的参数传入。

$ArrayHelper = new \Snscripts\ArrayHelper\ArrayHelper([
    'a' => 'Alpha',
    'b' => 'Bravo',
    'c' => 'Charlie',
    'd' => 'Delta'
]);

或者,如果对象已经创建,只需将数组传入 help 方法即可开始在该数组上工作。

$ArrayHelper->help(['replacement' => 'array']);

用法

除了 output()getOffsetByKey()getOffsetByValue() 之外的所有方法都返回 $this,以允许方法链。

output()

// Returns the array in it's current state
$ArrayHelper->output();

此方法简单地返回数组当前的状态。

clearArray()

// Clears empty() elements from the array (and any sub-arrays)
$ArrayHelper->clearArray();

此方法简单地删除数组中任何定义为“空”的元素,根据 PHP 的 Empty 方法。然后使用 output 方法返回清理后的数组。

splitArray()

此方法用于根据您想要的部分数量将数组分割成相等的(或尽可能接近的)部分。PHP 的 array_chunk 要求您为每个部分传递项目数量,这并不总是可能的。此方法会根据数组中的项目数量和所需的部分数量自动解决这个问题。

// Split the array into number of sections in equals amounts
// (Some sections may have extra elements due to remainders)
$ArrayHelper->splitArray(3);

示例

$ArrayHelper = new \Snscripts\ArrayHelper\ArrayHelper(
    [1, 2, 3, 4, 5, 6, 7]
);
$newArray = $ArrayHelper->splitArray(3)
    ->output();

/* $newArray
[
    [
        0 => 1,
        1 => 2,
        2 => 3
    ],
    [
        3 => 4,
        4 => 5
    ],
    [
        5 => 6,
        6 => 7
    ]
]
*/

addAfter()

此数组允许您根据元素键在数组中的任何位置添加多个项目。

// Adds the new array element after the given key (where ever it maybe within the array)
$ArrayHelper->addAfter('b', ['e' => 'Echo']);

示例

$ArrayHelper = new \Snscripts\ArrayHelper\ArrayHelper([
        'a' => 'foo',
        'b' => 'bar'
]);
$newArray = $ArrayHelper
    ->splitArray(
        'a', // after the item with key of 'a'
        [
            'c' => 'test'
        ]
    )
    ->output();

/* $newArray
[
    'a' => 'foo',
    'c' => 'test',
    'b' => 'bar'
]
*/

moveItem()

此方法允许您根据元素的键定义要移动的项目,并将其移动到指定元素键之后。

// Move an item within the array given the element key
$ArrayHelper->moveItem('e', 'd');

示例

$ArrayHelper = new \Snscripts\ArrayHelper\ArrayHelper([
    'a' => 'alpha',
    'b' => 'bravo',
    'c' => 'charlie'
]);
$newArray = $ArrayHelper
    ->moveItem(
        'b', // move element with key 'b'
        'c' // to after element with key 'c'
    )
    ->output();

/* $newArray
[
    'a' => 'alpha',
    'c' => 'charlie',
    'b' => 'bravo'
]
*/

cartesianProduct()

此方法将通过合并所有数组创建数组的笛卡尔积版本,以创建所有可能的组合。

在电子商务场景中非常有用,当需要创建基于尺寸/颜色或其他产品选项的所有可能产品选项的数组时。

// Create a cartesian product array
$ArrayHelper->cartesianProduct();

示例

$ArrayHelper = new \Snscripts\ArrayHelper\ArrayHelper([
    ['S', 'M', 'L'],
    ['Red', 'Blue']
]);
$newArray = $ArrayHelper->cartesianProduct()
    ->output();

/* $newArray
[
    ['S', 'Red'],
    ['S', 'Blue'],
    ['M', 'Red'],
    ['M', 'Blue'],
    ['L', 'Red'],
    ['L', 'Blue']
]
*/

getOffsetByKey()

此方法根据元素的键返回数组中元素的整数位置,如果不存在则返回 null

这对于使用需要偏移量值的例如 array_splice 的方法很有用,特别是与关联数组一起使用时。

// Return the offset position of an item where key matches
$ArrayHelper->getOffsetByKey('b');

示例

$ArrayHelper = new \Snscripts\ArrayHelper\ArrayHelper([
    'a' => 'Alpha',
    'b' => 'Bravo',
    'c' => 'Charlie'
]);
$offset = $ArrayHelper->getOffsetByKey('b');

// $offset = 1;

getOffsetByValue()

此方法的作用类似于 getOffsetByKey(),只是它根据项目值匹配项目并返回它在数组中的偏移量。

// Return the offset position of an item where value matches
$ArrayHelper->getOffsetByValue('Charlie');

示例

$ArrayHelper = new \Snscripts\ArrayHelper\ArrayHelper([
    'a' => 'Alpha',
    'b' => 'Bravo',
    'c' => 'Charlie'
]);
$offset = $ArrayHelper->getOffsetByValue('Charlie');

// $offset = 2;

贡献

有关详细信息,请参阅 CONTRIBUTING

许可证

MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件