PHP 简化的数组点表示法

v1.1.0 2016-07-07 04:14 UTC

This package is auto-updated.

Last update: 2024-09-08 06:52:26 UTC


README

Dot

Build Status Total Downloads Latest Stable Version

描述

Dot 是一个小的库,实现了由 Selvin Ortiz 编写的 PHP 的数组 点表示法

需求

安装

composer require selvinortiz/dot

测试

sh spec.sh

使用

在正确安装 Dot 后,只需 自动加载 它,使用 它,并在它上面调用方法即可:)

$input = [
    'name' => [
        'first' => 'Brad',
        'last'  => 'Bell',
    ],
    'spouse' => [
        'name' => [
            'first' => 'Brandon',
            'last'  => 'Kelly'
        ],
        'mood' => 'Happy',
        'age'  => '75',
    ],
    'mood' => 'Angry',
    'age'  => 25,
];

Dot::has($input, 'spouse');
// true

Dot::has($input, 'mistress.relationship');
// false

Dot::get($input, 'spouse.name.last');
// 'Kelly'

Dot::get($input, 'spouse');
/*
[
    'name' => [
        'first' => 'Brandon',
        'last'  => 'Kelly'
    ],
    'mood' => 'Happy',
    'age'  => '75'
]
*/

Dot::set($input, 'spouse.name.last', 'Bell');
/* $input will be mutated with a changed value
[
    'name' => [
        'first' => 'Brad',
        'last'  => 'Bell',
    ],
    'spouse' => [
        'name' => [
            'first' => 'Brandon',
            'last'  => 'Bell'
        ],
        'mood' => 'Happy',
        'age'  => '75',
    ],
    'mood' => 'Angry',
    'age'  => 25,
];
*/

Dot::delete($input, 'spouse.mood');
/* $input will be mutated with a key/value deleted
[
    'name' => [
        'first' => 'Brad',
        'last'  => 'Bell',
    ],
    'spouse' => [
        'name' => [
            'first' => 'Brandon',
            'last'  => 'Bell'
        ]
        'age'  => '75',
    ],
    'mood' => 'Angry',
    'age'  => 25,
];
*/

API

Dot 有一个非常小的 API,并希望保持它小而精炼。

Dot::has($arr, $key)

返回 $arr 是否包含 $key。换句话说,$key 是否存在于 $arr

Dot::get($arr, $key, $default = null)

通过 $key$arr 中返回找到的值或提供的 $default

Dot::set(array &$arr, $key, $value)

通过添加提供的 $key$value 来修改 $arr

Dot::delete(array &$arr, $key)

如果找到,通过删除 $key 和其关联的值来修改 $arr

贡献

Dot 希望对 第一次贡献者 友好。只需按照以下步骤操作,如果在过程中有任何问题,请随时联系。

  1. 创建分支!
  2. 创建您的 bugfixfeature 分支
  3. 提交并推送您的更改
  4. 提交 pull request

许可证

Dot 是开源软件,根据 MIT 许可证 许可