定制 opxcore/array 二次开发

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

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

opxcore/array

Composer 安装命令:

composer require opxcore/array

包简介

The OpxCore array utils.

README 文档

README

Build Status Coverage Status Latest Stable Version Total Downloads License

Installing

composer require opxcore/array

Usage

Available methods:

For dot notation:

  • OpxCore\Arr\Arr::dot($array)
  • OpxCore\Arr\Arr::get($array, $key, $default)
  • OpxCore\Arr\Arr::set($array, $key, $value)
  • OpxCore\Arr\Arr::has($array, $keys)
  • OpxCore\Arr\Arr::forget($array, $keys)
  • OpxCore\Arr\Arr::pull($array, $key, $default)
  • OpxCore\Arr\Arr::push($array, $key, $value)

Regular arrays only:

  • OpxCore\Arr\Arr::only($array, $keys)
  • OpxCore\Arr\Arr::first($array, $callback, $default)
  • OpxCore\Arr\Arr::last($array, $callback, $default)

All $default values are optional and could be callable that returns value. By default, it is always null.

Performance tips: All methods for manipulating with dot notation are optimised for performance on a dot notated keys. If you are care about performance much it is better to use regular array manipulations on non-dot notated arrays.

Making a dot notated array

$dotNotated = Arr::dot($array);

Makes a dot notated array from any flat or multidimensional array.

    $array = ['level1' => ['level2' => 'value']];
    
    $result = Arr::dot($array);
    // $result === ['level1.level2' => 'value']

Getting a value using dot notation

OpxCore\Arr\Arr::get($array, $key, $default = null)

Gets a value from the array using dot notation. If the given key is not existing in the array, this function will return default value.

    use OpxCore\Arr\Arr;

    $array = ['level1' => ['level2' => 'value']];
    
    $result = Arr::get($array, 'level1.level2');
    // $result === 'value'

    $result = Arr::get($array, 'level1.level3');
    // $result === null

    $result = Arr::get($array, 'level1.level3', false);
    // $result === false

    $result = Arr::get($array, 'level1.level3', function(){return -1;});
    // $result === -1

Setting a value using dot notation

OpxCore\Arr\Arr::set($array, $key, $value): array

Sets the value into the array for the given key using dot notation. Function modifies given array directly, but also returns modified array for convenient usage.

    use OpxCore\Arr\Arr;
    
    $array = ['level1' => ['level2' => 'value']];
    
    $result = Arr::set($array, 'level1.level2_1', 'another value');
    // $result === ['level1' => ['level2' => 'value', 'level2_1' => 'another value']]
    // $array === $result

Determining a key (or keys) existence in the array

OpxCore\Arr\Arr::has($array, $keys)

Determines if the given key (or several keys) are existing in the array.

    use OpxCore\Arr\Arr;
    
    $array = ['level1' => ['level2' => 'value', 'level2_1' => 'another value']];
    
    $result = Arr::has($array, 'level1.level2');
    // $result === true
    
    $result = Arr::has($array, ['level1.level2', 'level1.level2_2']);
    // $result === false

Removing the values associated with the given keys

OpxCore\Arr\Arr::forget($array, $keys)

Removes the given key (or several keys given as an array) from the array. If any of keys are not existing in the array nothing happens. Function modifies given array directly, but also returns modified array for convenient usage.

    use OpxCore\Arr\Arr;
    
    $array = ['level1' => ['level2' => 'value', 'level2_1' => 'another value']];
    
    $result = Arr::forget($array, 'level1.level2');
    // $result === ['level1' => ['level2_1' => 'another value']]
    // $array === $result

Pulling the value from the array

OpxCore\Arr\Arr::pull($array, $key, $default)

Actually, this is combination of Arr::get() and Arr::forget() methods. This method fetches the value and then removes it from the array. If the key is not exists in the array, default value will be returned.

    use OpxCore\Arr\Arr;
    
    $array = ['level1' => ['level2' => 'value', 'level2_1' => 'another value']];
    
    $result = Arr::pull($array, 'level1.level2');
    // $result === 'value' 
    // $array === ['level1' => ['level2_1' => 'another value']]

Pushing the value into the array

OpxCore\Arr\Arr::push($array, $key, $value)

Pushes the value into the given key in array. If given key is not existing, it will be created and the value will be set as item of unassociated array. If the key is existing, value associated with this key will be casted as array and value will be added into it. Function modifies given array directly, but also returns modified array for convenient usage.

        use OpxCore\Arr\Arr;
        
        $array = ['level1' => ['level2' => 'value']];
        
        $result = Arr::push($array, 'level1.level2', 'another value');
        // $result === ['level1' => ['level2' => ['value', 'another value']]] 
        // $array === $result

opxcore/array 适用场景与选型建议

opxcore/array 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 141 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 01 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-01-13