定制 hoffmann-oss/arrays 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hoffmann-oss/arrays

Composer 安装命令:

composer require hoffmann-oss/arrays

包简介

PHP 7 array functions

README 文档

README

A PHP 7 array manipulation library.

Installation with Composer

This package is available on packagist, so you can use Composer to install it. Run the following command in your shell:

composer require hoffmann-oss/arrays

The Arrays class

create()

public static function &create(int $size, $item = null): array

Example:

$array = Arrays::create(5, 1000);

the resulting array:

[1000, 1000, 1000, 1000, 1000]

fill()

public static function fill(
    array &$array,
    $item = null,
    int $start = 0,
    int $stop = null
)

Example:

$array1 = $array2 = [1, 2, 3, 4, 5, 6, 7, 8];
Arrays::fill($array1, 1000);
Arrays::fill($array2, 1000, 2, 6);

the resulting arrays:

[1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000]
[1, 2, 1000, 1000, 1000, 1000, 7, 8]

replace()

public static function replace(
    array &$array,
    array $source,
    int $position = 0,
    int $start = 0,
    int $stop = null
)

Example:

$array1 = $array2 = [1, 2, 3, 4, 5, 6, 7, 8];
$source = [1001, 1002, 1003, 1004, 1005];
Arrays::replace($array1, $source, 4);
Arrays::replace($array2, $source, 4, 2, 6);

the resulting arrays:

[1, 2, 3, 4, 1001, 1002, 1003, 1004]
[1, 2, 3, 4, 1001, 1002, 7, 8]

reverse()

public static function reverse(
    array &$array,
    int $start = 0,
    int $stop = null
)

Example:

$array1 = $array2 = [1, 2, 3, 4, 5, 6, 7, 8];
Arrays::reverse($array1);
Arrays::reverse($array2, 2, 6);

the resulting arrays:

[8, 7, 6, 5, 4, 3, 2, 1]
[1, 2, 6, 5, 4, 3, 7, 8]

rotate()

public static function rotate(
    array &$array,
    int $distance,
    int $start = 0,
    int $stop = null
)

Example:

$array1 = $array2 = [1, 2, 3, 4, 5, 6, 7, 8];
Arrays::rotate($array1, -2);
Arrays::rotate($array2, 2, 2, 6);

the resulting arrays:

[7, 8, 1, 2, 3, 4, 5, 6]
[1, 2, 5, 6, 3, 4, 7, 8]

shuffle()

public static function shuffle(
    array &$array,
    int $start = 0,
    int $stop = null
)

Example:

$array1 = $array2 = [1, 2, 3, 4, 5, 6, 7, 8];
Arrays::shuffle($array1);
Arrays::shuffle($array2, 2, 6);

the resulting arrays:

[4, 6, 1, 2, 8, 7, 5, 3]
[1, 2, 3, 5, 6, 4, 7, 8]

swap()

public static function swap(
    array &$array,
    int $index1,
    int $index2
)

Example:

$array = [1, 2, 3, 4];
Arrays::swap($array, 0, 1);
Arrays::swap($array, 1, 2);
Arrays::swap($array, 2, 3);

the resulting array:

[2, 3, 4, 1]

pick()

public static function pick(
    array $array,
    int $start = 0,
    int $stop = null
)

Example:

$source = [1, 2, 3, 4, 5, 6, 7, 8];
$array1 = $array2 = [];
for ($i = 0; $i < 10; $i++) {
    $array1[] = Arrays::pick($source);
    $array2[] = Arrays::pick($source, 2, 6);
}

the resulting arrays:

[8, 2, 1, 3, 3, 7, 3, 1, 7, 6]
[5, 3, 4, 6, 5, 4, 6, 3, 3, 6]

The Arrays2d class

The functions of this class assumes that the parameter $array is a rectangular array (y) of arrays (x).

create()

public static function &create(int $width, int $height, $item = null): array

Example:

$array = Arrays2d::create(3, 3, 1000);

the resulting array:

[
    [1000, 1000, 1000],
    [1000, 1000, 1000],
    [1000, 1000, 1000]
]

fill()

public static function fill(
    array &$array,
    $item = null,
    int $startX = 0,
    int $startY = 0,
    int $stopX = null,
    int $stopY = null
)

Example:

$array1 = $array2 = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];
Arrays2d::fill($array1, 1000);
Arrays2d::fill($array2, 1000, 1, 0, 3, 2);

the resulting arrays:

[
    [1000, 1000, 1000],
    [1000, 1000, 1000],
    [1000, 1000, 1000]
]
[
    [1, 1000, 1000],
    [4, 1000, 1000],
    [7, 8, 9]
]

replace()

public static function replace(
    array &$array,
    array $source,
    int $x = 0,
    int $y = 0,
    int $startX = 0,
    int $startY = 0,
    int $stopX = null,
    int $stopY = null
)

Example:

$array1 = $array2 = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];
$source = [
    [1001, 1002, 1003],
    [1004, 1005, 1006],
    [1007, 1008, 1009]
];
Arrays2d::replace($array1, $source, 0, 0);
Arrays2d::replace($array2, $source, 0, 0, 1, 0, 3, 2);

the resulting arrays:

[
    [1001, 1002, 1003],
    [1004, 1005, 1006],
    [1007, 1008, 1009]
]
[
    [1, 1002, 1003],
    [4, 1005, 1006],
    [7, 8, 9]
]

reverseX()

public static function reverseX(
    array &$array,
    int $startX = 0,
    int $startY = 0,
    int $stopX = null,
    int $stopY = null
)

Example:

$array1 = $array2 = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12]
];
Arrays2d::reverseX($array1);
Arrays2d::reverseX($array2, 1, 0, 4, 2);

the resulting arrays:

[
    [4, 3, 2, 1],
    [8, 7, 6, 5],
    [12, 11, 10, 9]
]
[
    [1, 4, 3, 2],
    [5, 8, 7, 6],
    [9, 10, 11, 12]
]

reverseY()

public static function reverseY(
    array &$array,
    int $startX = 0,
    int $startY = 0,
    int $stopX = null,
    int $stopY = null
)

Example:

$array1 = $array2 = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
    [10, 11, 12]
];
Arrays2d::reverseY($array1);
Arrays2d::reverseY($array2, 1, 0, 3, 3);

the resulting arrays:

[
    [10, 11, 12],
    [7, 8, 9],
    [4, 5, 6],
    [1, 2, 3]
]
[
    [1, 8, 9],
    [4, 5, 6],
    [7, 2, 3],
    [10, 11, 12]
]

rotateX()

public static function rotateX(
    array &$array,
    int $distance,
    int $startX = 0,
    int $startY = 0,
    int $stopX = null,
    int $stopY = null
)

Array columns rotate left if distance is greater than 0.

Example:

$array1 = $array2 = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12]
];
Arrays2d::rotateX($array1, 1);
Arrays2d::rotateX($array2, -1, 1, 0, 4, 2);

the resulting arrays:

[
    [2, 3, 4, 1],
    [6, 7, 8, 5],
    [10, 11, 12, 9]
]
[
    [1, 4, 2, 3],
    [5, 8, 6, 7],
    [9, 10, 11, 12]
]

rotateY()

public static function rotateY(
    array &$array,
    int $distance,
    int $startX = 0,
    int $startY = 0,
    int $stopX = null,
    int $stopY = null
)

Array rows rotate up if distance is greater than 0.

Example:

$array1 = $array2 = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
    [10, 11, 12]
];
Arrays2d::rotateY($array1, 1);
Arrays2d::rotateY($array2, -1, 1, 0, 3, 3);

the resulting arrays:

[
    [4, 5, 6],
    [7, 8, 9],
    [10, 11, 12],
    [1, 2, 3]
]
[
    [1, 8, 9],
    [4, 2, 3],
    [7, 5, 6],
    [10, 11, 12]
]

shuffle()

public static function shuffle(
    array &$array,
    int $startX = 0,
    int $startY = 0,
    int $stopX = null,
    int $stopY = null
)

Example:

$array1 = $array2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
Arrays2d::shuffle($array1);
Arrays2d::shuffle($array2, 1, 0, 3, 2);

the resulting arrays:

[
    [5, 2, 9],
    [3, 1, 4],
    [7, 8, 6]
]
[
    [1, 5, 3],
    [4, 6, 2],
    [7, 8, 9]
]

shuffleX()

public static function shuffleX(
    array &$array,
    int $startX = 0,
    int $startY = 0,
    int $stopX = null,
    int $stopY = null
)

Example:

$array1 = $array2 = [
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10],
    [11, 12, 13, 14, 15]
];
Arrays2d::shuffleX($array1);
Arrays2d::shuffleX($array2, 1, 0, 5, 2);

the resulting arrays:

[
    [3, 5, 1, 2, 4],
    [8, 10, 6, 7, 9],
    [13, 15, 11, 12, 14]
]
[
    [1, 5, 4, 2, 3],
    [6, 10, 9, 7, 8],
    [11, 12, 13, 14, 15]
]

shuffleY()

public static function shuffleY(
    array &$array,
    int $startX = 0,
    int $startY = 0,
    int $stopX = null,
    int $stopY = null
)

Example:

$array1 = $array2 = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
    [10, 11, 12],
    [13, 14, 15]
];
Arrays2d::shuffleY($array1);
Arrays2d::shuffleY($array2, 1, 0, 3, 4);

the resulting arrays:

[
    [4, 5, 6],
    [1, 2, 3],
    [10, 11, 12],
    [13, 14, 15],
    [7, 8, 9]
]
[
    [1, 11, 12],
    [4, 5, 6],
    [7, 8, 9],
    [10, 2, 3],
    [13, 14, 15]
]

swap()

public static function swap(
    array &$array,
    int $x1,
    int $y1,
    int $x2,
    int $y2
)

Example:

$array = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];
Arrays2d::swap($array, 0, 1, 2, 0);
Arrays2d::swap($array, 0, 2, 2, 1);

the resulting array:

[
    [1, 2, 4],
    [3, 5, 7],
    [6, 8, 9]
]

swapX()

public static function swapX(
    array &$array,
    int $x1,
    int $x2,
    int $startY = 0,
    int $stopY = null
)

Example:

$array1 = $array2 = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12]
];
Arrays2d::swapX($array1, 2, 3);
Arrays2d::swapX($array2, 2, 3, 0, 2);

the resulting arrays:

[
    [1, 2, 4, 3],
    [5, 6, 8, 7],
    [9, 10, 12, 11]
]
[
    [1, 2, 4, 3],
    [5, 6, 8, 7],
    [9, 10, 11, 12]
]

swapY()

public static function swapY(
    array &$array,
    int $y1,
    int $y2,
    int $startX = 0,
    int $stopX = null
)

Example:

$array1 = $array2 = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
    [10, 11, 12]
];
Arrays2d::swapY($array1, 2, 3);
Arrays2d::swapY($array2, 2, 3, 0, 2);

the resulting arrays:

[
    [1, 2, 3],
    [4, 5, 6],
    [10, 11, 12],
    [7, 8, 9]
]
[
    [1, 2, 3],
    [4, 5, 6],
    [10, 11, 9],
    [7, 8, 12]
]

pick()

public static function pick(
    array $array,
    int $startX = 0,
    int $startY = 0,
    int $stopX = null,
    int $stopY = null
)

Example:

$source = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];
$array1 = $array2 = [];
for ($i = 0; $i < 10; $i++) {
    $array1[] = Arrays2d::pick($source);
    $array2[] = Arrays2d::pick($source, 0, 0, 2, 2);
}

the resulting arrays:

[5, 7, 8, 9, 2, 2, 6, 7, 5, 2]
[1, 2, 1, 4, 4, 4, 1, 2, 1, 2]

hoffmann-oss/arrays 适用场景与选型建议

hoffmann-oss/arrays 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 356 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 11 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「utility」 「arrays」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 hoffmann-oss/arrays 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 hoffmann-oss/arrays 我们能提供哪些服务?
定制开发 / 二次开发

基于 hoffmann-oss/arrays 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 356
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-11-17