定制 smoren/array-view 二次开发

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

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

smoren/array-view

Composer 安装命令:

composer require smoren/array-view

包简介

Create array views for easy data manipulation, select elements using Python-like slice notation, enable efficient selection of elements using index lists and boolean masks.

README 文档

README

Packagist PHP Version Support Scrutinizer Code Quality Coverage Status Build and test License: MIT

Array View Logo

Array View is a PHP library that provides powerful abstractions and utilities for working with lists of data. Create views of arrays, slice and index using Python-like notation, transform and select your data using chained and fluent operations.

Features

  • Array views as an abstraction over an array
  • Forward and backward array indexing
  • Selecting and slicing using Python-like slice notation
  • Filtering, mapping, matching and masking
  • Chaining operations via pipes and fluent interfaces

How to install to your project

composer require smoren/array-view

Usage

Indexing

Index into an array forward or backwards using positive or negative indexes.

Data 1 2 3 4 5 6 7
Positive Index 0 1 2 3 4 5 6
Negative Index -7 -6 -5 -4 -3 -2 -1
use Smoren\ArrayView\Views\ArrayView;

$view = ArrayView::toView([1, 2, 3, 4, 5, 6, 7]);

$view[0];  // 1
$view[1];  // 2
$view[-1]; // 7
$view[-2]; // 6

Slices

Use Python-like slice notation to select a range of elements: [start, stop, step].

use Smoren\ArrayView\Views\ArrayView;

$originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$view = ArrayView::toView($originalArray);

$view['1:6'];   // [2, 3, 4, 5, 6]
$view['1:7:2']; // [2, 4, 6]
$view[':3'];    // [1, 2, 3]
$view['::-1'];  // [9, 8, 7, 6, 5, 4, 3, 2, 1]

Insert into parts of the array.

$view['1:7:2'] = [22, 44, 66];
print_r($originalArray); // [1, 22, 3, 44, 5, 66, 7, 8, 9]

Subviews

Create subviews of the original view using masks, indexes, and slices.

use Smoren\ArrayView\Selectors\IndexListSelector;
use Smoren\ArrayView\Selectors\MaskSelector;
use Smoren\ArrayView\Selectors\SliceSelector;
use Smoren\ArrayView\Views\ArrayView;

$originalArray = [1, 2, 3, 4, 5];
$view = ArrayView::toView($originalArray);

// Object-oriented style
$view->subview(new MaskSelector([true, false, true, false, true]))->toArray(); // [1, 3, 5]
$view->subview(new IndexListSelector([1, 2, 4]))->toArray();                   // [2, 3, 5]
$view->subview(new SliceSelector('::-1'))->toArray();                          // [5, 4, 3, 2, 1]

// Scripting style 
$view->subview([true, false, true, false, true])->toArray(); // [1, 3, 5]
$view->subview([1, 2, 4])->toArray();                        // [2, 3, 5]
$view->subview('::-1')->toArray();                           // [5, 4, 3, 2, 1]

$view->subview(new MaskSelector([true, false, true, false, true]))->apply(fn ($x) => x * 10);
print_r($originalArray); // [10, 2, 30, 4, 50]

Subarray Multi-indexing

Directly select multiple elements using an array-index multi-selection.

use Smoren\ArrayView\Selectors\IndexListSelector;
use Smoren\ArrayView\Selectors\MaskSelector;
use Smoren\ArrayView\Selectors\SliceSelector;
use Smoren\ArrayView\Views\ArrayView;

$originalArray = [1, 2, 3, 4, 5];
$view = ArrayView::toView($originalArray);

// Object-oriented style
$view[new MaskSelector([true, false, true, false, true])]; // [1, 3, 5]
$view[new IndexListSelector([1, 2, 4])];                   // [2, 3, 5]
$view[new SliceSelector('::-1')];                          // [5, 4, 3, 2, 1]

// Scripting style
$view[[true, false, true, false, true]]; // [1, 3, 5]
$view[[1, 2, 4]];                        // [2, 3, 5]
$view['::-1'];                           // [5, 4, 3, 2, 1]

$view[new MaskSelector([true, false, true, false, true])] = [10, 30, 50];
print_r($originalArray); // [10, 2, 30, 4, 50]

Combining Subviews

Combine and chain subviews one after another in a fluent interface to perform multiple selection operations.

use Smoren\ArrayView\Selectors\IndexListSelector;
use Smoren\ArrayView\Selectors\MaskSelector;
use Smoren\ArrayView\Selectors\SliceSelector;
use Smoren\ArrayView\Views\ArrayView;

$originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// Fluent object-oriented style
$subview = ArrayView::toView($originalArray)
    ->subview(new SliceSelector('::2'))                          // [1, 3, 5, 7, 9]
    ->subview(new MaskSelector([true, false, true, true, true])) // [1, 5, 7, 9]
    ->subview(new IndexListSelector([0, 1, 2]))                  // [1, 5, 7]
    ->subview(new SliceSelector('1:'));                          // [5, 7]

$subview[':'] = [55, 77];
print_r($originalArray); // [1, 2, 3, 4, 55, 6, 77, 8, 9, 10]

// Fluent scripting style
$originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$subview = ArrayView::toView($originalArray)
    ->subview('::2')                           // [1, 3, 5, 7, 9]
    ->subview([true, false, true, true, true]) // [1, 5, 7, 9]
    ->subview([0, 1, 2])                       // [1, 5, 7]
    ->subview('1:');                           // [5, 7]

$subview[':'] = [55, 77];
print_r($originalArray); // [1, 2, 3, 4, 55, 6, 77, 8, 9, 10]

Selectors Pipe

Create pipelines of selections that can be saved and applied again and again to new array views.

use Smoren\ArrayView\Selectors\IndexListSelector;
use Smoren\ArrayView\Selectors\MaskSelector;
use Smoren\ArrayView\Selectors\SliceSelector;
use Smoren\ArrayView\Views\ArrayView;

$originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$selector = new PipeSelector([
    new SliceSelector('::2'),
    new MaskSelector([true, false, true, true, true]),
    new IndexListSelector([0, 1, 2]),
    new SliceSelector('1:'),
]);

$view = ArrayView::toView($originalArray);
$subview = $view->subview($selector);
print_r($subview[':']); // [5, 7]

$subview[':'] = [55, 77];
print_r($originalArray); // [1, 2, 3, 4, 55, 6, 77, 8, 9, 10]

Documentation

For detailed documentation and usage examples, please refer to the API documentation.

Unit testing

composer install
composer test-init
composer test

Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request on the GitHub repository.

Standards

ArrayView conforms to the following standards:

License

ArrayView PHP is licensed under the MIT License.

smoren/array-view 适用场景与选型建议

smoren/array-view 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 12, 最近一次更新时间为 2024 年 03 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 smoren/array-view 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 9
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 12
  • 点击次数: 29
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-10