定制 mathiasgrimm/arraypath 二次开发

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

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

mathiasgrimm/arraypath

最新稳定版本:v2.0.7

Composer 安装命令:

composer require mathiasgrimm/arraypath

包简介

php array utility to manipulate array in a xpath way

README 文档

README

Author Latest Version Total Downloads

ArrayPath is an easy and very convenient way for manipulating arrays, especially multidimensional.
Forget about checking for existing indexes and/or getting an E_NOTICE.

With ArrayPath you can easily Check, Add, Remove and Retrieve elements from any array

Our examples will be using a class alias which will be explained next

Using Class Alias

The default class alias is A but you can also define your custom alias.

By using the default alias you get the benefits of the ide auto-completion

<?php
// recommended
ArrayPath::registerClassAlias();

A::get($aData, 'a/b/c');

// or

ArrayPath::registerClassAlias('MyAlias');
MyAlias::get($aData, 'a/b/c');

A good place to register the class alias is in any bootstrap file like a Service Provider or initialisation script

Example 1 (Get)

<?php
$post = array(
	'user' => array(
	    'basicInformation' => array(
	        'name'    => 'Mathias',
	        'surname' => 'Grimm'
	    ),
	)
);

// normal php way
$sName    = isset($post['user']['basicInformation']['name'   ]) ? $post['user']['basicInformation']['name'   ] : null;
$sSurname = isset($post['user']['basicInformation']['surname']) ? $post['user']['basicInformation']['surname'] : null;

// default value
$sLocale = isset($post['user']['locale']) ? $post['user']['locale'] : 'Europe/Dublin';

// ===================================================================

// ArrayPath
$sName    = A::get($post, 'user/basicInformation/name');
$sSurname = A::get($post, 'user/basicInformation/surname');

// with default value
$sLocale  = A::get($post, 'user/locale', 'Europe/Dublin');

Example 2 (Set)

<?php
// normal php way
$aUser = array();
$sName = $aUser['user']['basicInformation']['name'] = 'Mathias Grimm';
// ===================================================================

// ArrayPath
$aUser = array();
$sName = A::set($aUser, 'user/basicInformation/name', 'Mathias');

Example 3 (Exists)

The exists checks for the existence of the index as in array_key_exists
and returns true if the key exists regardless the value.
An isset will return false in case of a null value.

<?php
// normal php way
$bExists = false;
if (array_key_exists('user', (array) $aUser)) {
	if (array_key_exists('basicInformation', (array) $aUser['user'])) {
		if (array_key_exists('name', (array) $aUser['user']['basicInformation'])) {
			$bExists = true;
		}
	}
}

// ===================================================================

// ArrayPath
$bExists = A::exists($aUser, 'user/basicInformation/name');

Example 4 (Get and Remove)

<?php
// normal php way
if (isset($aUser['user']['basicInformation']['name'])) {
	$sName = $aUser['user']['basicInformation']['name'];
	unset($aUser['user']['basicInformation']['name']);
}


// ArrayPath
$sName = A::remove($aUser, 'user/basicInformation/name');

Example 5 (Using a custom separator)

<?php
ArrayPath::setSeparator('.');
$sName = A::get($aUser, 'user.basicInformation.name');

ArrayPath::setSeparator('-');
$sName = A::get($aUser, 'user-basicInformation-name');

ArrayPath::setSeparator('->');
$sName = A::get($aUser, 'user->basicInformation->name');

ArrayPath::setSeparator('|');
$sName = A::get($aUser, 'user|basicInformation|name');

Parameters Consistency

The parameters will always be in this sequence, when available:

$arrayData, $index, $value, $default

Composer/Packagist

https://packagist.org/packages/mathiasgrimm/arraypath

"require": {
    "mathiasgrimm/arraypath": "2.*"
}

mathiasgrimm/arraypath 适用场景与选型建议

mathiasgrimm/arraypath 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26.53k 次下载、GitHub Stars 达 25, 最近一次更新时间为 2014 年 09 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 25
  • Watchers: 4
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2014-09-29