承接 igorw/get-in 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

igorw/get-in

最新稳定版本:v1.0.3

Composer 安装命令:

composer require igorw/get-in

包简介

Functions for hash map (assoc array) traversal.

README 文档

README

Build Status

Functions for hash map (assoc array) traversal.

When dealing with nested associative structures, traversing them can become quite a pain. Mostly because of the amount of isset checking that is necessary.

For example, to access a nested key ['foo']['bar']['baz'], you must do something like this:

$baz = (isset($data['foo']['bar']['baz'])) ? $data['foo']['bar']['baz'] : null;

Enough already! get-in provides a better way:

$baz = igorw\get_in($data, ['foo', 'bar', 'baz']);

Installation

Through composer:

$ composer require igorw/get-in:~1.0

Usage

get_in

Retrieve value from a nested structure using a list of keys:

$users = [
    ['name' => 'Igor Wiedler'],
    ['name' => 'Jane Doe'],
    ['name' => 'Acme Inc'],
];

$name = igorw\get_in($users, [1, 'name']);
//= 'Jane Doe'

Non existent keys return null:

$data = ['foo' => 'bar'];

$baz = igorw\get_in($data, ['baz']);
//= null

You can provide a default value that will be used instead of null:

$data = ['foo' => 'bar'];

$baz = igorw\get_in($data, ['baz'], 'qux');
//= 'qux'

update_in

Apply a function to the value at a particular location in a nested structure:

$data = ['foo' => ['answer' => 42]];
$inc = function ($x) {
    return $x + 1;
};

$new = igorw\update_in($data, ['foo', 'answer'], $inc);
//= ['foo' => ['answer' => 43]]

You can variadically provide additional arguments for the function:

$data = ['foo' => 'bar'];
$concat = function (/* $args... */) {
    return implode('', func_get_args());
};

$new = igorw\update_in($data, ['foo'], $concat, ' is the ', 'best');
//= ['foo' => 'bar is the best']

assoc_in

Set a value at a particular location:

$data = ['foo' => 'bar'];

$new = igorw\assoc_in($data, ['foo'], 'baz');
//= ['foo' => 'baz']

It will also set the value if it does not exist yet:

$data = [];

$new = igorw\assoc_in($data, ['foo', 'bar'], 'baz');
//= ['foo' => ['bar' => 'baz']]

Inspiration

The naming and implementation is inspired by the get-in, update-in and assoc-in functions from clojure.

统计信息

  • 总下载量: 12.44M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 394
  • 点击次数: 6
  • 依赖项目数: 26
  • 推荐数: 0

GitHub 信息

  • Stars: 392
  • Watchers: 8
  • Forks: 23
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04