定制 oliverde8/associative-array-simplified 二次开发

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

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

oliverde8/associative-array-simplified

Composer 安装命令:

composer require oliverde8/associative-array-simplified

包简介

Allow simplified manupulation of recursive associative arrays.

README 文档

README

This library allow simplified manipulation of recursive associative arrays with an object oriented aproach in php.

Latest Stable Version Build Status Scrutinizer Code Quality Code Coverage Total Downloads License

SensioLabsInsight

Associative Array

This is the main clas of the library. It allows the manipulation of the association arrays with ease.

This allow you to get data from an associative array with ease. This can be used many places even for getting information from SESSION or POST & GET values.

Typically when we try and get information from a an array of which we don't know the content we need to write a lot of conditions :

$array = ['a' => ['b' => 2, 'c' => 3]];
if (isset($array['a'])) {
    $valB = isset($array['a']['b']) ? $array['a']['b'] : "default";
    $valB = isset($array['a']['c']) ? $array['a']['c'] : "default";
}

After a certain while we write a lot conditions and the array is very nested it becomes difficult to understand. This library makes it much easier to write code of this type & makes it easier to read and understand. The code above is simply replaced by :

$object = new AssociativeArray( ['a' => ['b' => 2, 'c' => 3]]);
$valB = $object->get('a/b', 'default');
$valB = $object->get('a/c', 'default');

Requirments

  • PHP 5.4 or higher.

Installation

You should always install the library using composer.

composer require oliverde8/associative-array-simplified

Manual installation is not recomended and not supported.

Usage Exemples

Get values from a simple array

$array = ['a' => 1, 'b' => 2,];
$object = new AssociativeArray($array);

$object->get('a');
//This will return '1'

$object->get('c');
//This will return null

Get a value and if no value is set get a default one

$object->get('c', 10);
//This will return 10

Get nested values recursively

$array = ['a' => ['b' => 2]];
$object = new AssociativeArray($array);

$object->get('a');
//This will return ['b' => 2]

$object->get(['a', 'b']);
//This will return 2

And of course you can continue to use default values with the recursive get.

Use separators in the string keys to get nested values

The default separator is /

$object->get('a/b');
//This will return 2

You can set new values or replace them.

// Both of these do the same thing.
$object->set('a/d', 10);
$object->set(['a','d'], 10);

You can get back your array

// And you get back the array.
$object->array()

Using custom separators

When creating the AssociativeArray you can specify the separator to use. So instead of

$array = ['a' => ['b' => 2]];
$object = new AssociativeArray($array);
$this->get('a/b');

You can write

$array = ['a' => ['b' => 2]];
$object = new AssociativeArray($array, '.');
$this->get('a.b');

Example usage of static methods

The class has also static methods to manipulate your array directly. The method of the objects actually uses these static methods to manipulate the array.

$array = ['a' => ['b' => 2]];
AssociativeArray::getFromKey($array, 'a/b'); 
// Will return 2

And to set new data.

$array = ['a' => ['b' => 2]];
AssociativeArray::setFromKey($array, 'a/c', 'yop); 
// Will return ['a' => ['b' => 2, 'c' => 'yop']] 

Other functions

The class has also other functions.

  • clear : Empties all the data from the array.
  • keyExist : Checks if a key exists. (value may be null)

FAQ

Why don't the associative array has an iterator ?

Associative Array Simplified doesen't try and replace arrays; other libraries were designed to do so. This library simply makes it easier yo access values in associative arrays.

Why don't the associative array return associative array instead of normal array's ?

This is something I spent quite some time to decide, but doing it that way would require during the creation of the associative array to process the array to create the nested associative arrays. This would have slowned then the process of creation without really increasing perormance at any other level. On long term I might create a second object working that way.

There is some questions about this how should this work :

$array = ['a' => ['b' => 2]];
$object = new AssociativeArray($array);

$object->get('a');
//This will return ['b' => 2]

$object->get(a)->get('b');
//Should return 2

$object->get(a)->get('b')->get('c', 'default');
//Will make an error but should have returned 'default'

So there is some reflexion to be done on the subject.

Why don't the assotiative array use references ?

It's planned. Current implementation is slower as it duplicated the arrays during the set operations.

...

TODO

  • Add some more options (clear, disable new data ...)
  • uses references to use less memory.
  • create second version returning Associative Array instead of normal arrays.
  • Something else? Open a ticket :)

oliverde8/associative-array-simplified 适用场景与选型建议

oliverde8/associative-array-simplified 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.05k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 02 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-02-17