sajadsdi/array-dot-notation

使用点表示法在数组中设置和获取值

1.0.5 2023-12-12 02:38 UTC

This package is auto-updated.

Last update: 2024-09-12 04:21:07 UTC


README

PHP Array Dot Notation

数组点表示法

数组点表示法是一个高性能且轻量级的PHP库,允许您使用点表示法在数组中设置和获取值。它通过提供直观的方式来访问和修改数据,简化了处理嵌套数组。

特性

  • 使用点表示法键从数组中获取值。
  • 使用点表示法键在数组中设置值。
  • 使用点表示法从数组中删除键。
  • 使用点表示法检查键或多个键在数组中是否存在。
  • 检查键列表中是否存在数组中的一个键...
  • 在获取操作中对输出数据映射键。
  • get 函数的单个和多个默认值
  • get 函数中每个键返回默认值时的回调
  • set 函数中设置键时的回调

安装

您可以使用Composer安装此库。如果您还没有为项目设置Composer,可以按照官方Composer安装指南进行操作。

Composer安装后,运行以下命令安装Array Dot Notation

composer require sajadsdi/array-dot-notation

用法

基本用法

以下是您如何在PHP项目中使用Array Dot Notation的示例

<?php
require 'vendor/autoload.php';

use Sajadsdi\ArrayDotNotation\DotNotation;

// Create an instance of DotNotation with an array
$data = [
    'user' => [
        'profile' => [
            'id' => 625,
            'pic' => '625.png',
        ],
    ],
];

$dotNotation = new DotNotation($data);

// Get values using dot notation
$userId = $dotNotation->get('user.profile.id'); // $userId will be 625
$picPath = $dotNotation->get('user.profile.pic'); // $picPath will be '625.png'

// Check if a key exists
if ($dotNotation->has('user.profile.id')) {
    // Key exists
} else {
    // Key does not exist
}

// Set values using dot notation
$dotNotation->set(['user.profile.id' => 12345, 'user.profile.pic' => 'new_pic.png']);

// Get the updated value
$newUserId = $dotNotation->get('user.profile.id'); // $newUserId will be 12345

// Delete key(s) using dot notation
$dotNotation->delete('user.profile.id');

$user = $dotNotation->get();
/** The $user will be :
[
    'user' => [
        'profile' => [
            'pic' => 'new_pic.png'
        ]
    ]
]
*/
//set again
$dotNotation->set(['user.profile.id' => 1234, 'user.profile.pic' => 'new_pic2.png']);
//multi keys deletion
$dotNotation->delete(['user.profile.id','user.profile.pic']);

$user = $dotNotation->get();
/** The $user will be :
[
    'user' => [
        'profile' => []
    ]
]
*/

高级用法

示例 1:同时获取多个值

您可以使用点表示法同时检索多个值

$settings = [
    'app' => [
        'name' => 'My App',
        'version' => '1.0',
    ],
    'user' => [
        'theme' => 'light',
    ],
];

// Initialize DotNotation with the $settings array
$dotNotation = new DotNotation($settings);

$result = $dotNotation->get(['app.name', 'app.version', 'user']);
// The result will be ['name' => 'My App', 'version' => '1.0', 'user' => ['theme' => 'light']]

示例 2:在获取时映射输出数据键

您可以在使用get方法时映射输出数据键

$data = [
    'user' => [
        'profile' => [
            'id' => 625,
            'pic' => '625.png',
        ],
    ],
];

// Initialize DotNotation with the $data array
$dotNotation = new DotNotation($data);

$keys = ['profile_id' => 'user.profile.id', 'profile_photo' => 'user.profile.pic'];

$result = $dotNotation->get($keys);
// The result will be ['profile_id' => 625, 'profile_photo' => '625.png']

示例 3:自动处理重复键

当您不使用映射获取值时,库将自动处理重复键

$array = [
    'users' => [
        ['id' => 1, 'name' => 'John'],
        ['id' => 2, 'name' => 'Alice'],
        ['id' => 3, 'name' => 'Emma'],
        ['id' => 4, 'name' => 'Emily'],
        ['id' => 5, 'name' => 'Sofia'],
    ],
];

// Initialize DotNotation with the $array
$dotNotation = new DotNotation($array);

$keys = ['users.2.name', 'users.3.name', 'users.4.name'];

$result = $dotNotation->get($keys);
// The result will be ['name' => 'Emma', 'name_1' => 'Emily', 'name_2' => 'Sofia']

// The keys are ['users.2.name', 'users.3.name', 'users.4.name'], in fact [0 => 'users.2.name', 1 =>'users.3.name', 2 => 'users.4.name']

示例 4:自动处理数字键

当最后一个键是数字索引时,它将使用输入数组的索引

$array = [
    'users' => [
        ['id' => 1, 'name' => 'John'],
        ['id' => 2, 'name' => 'Alice'],
        ['id' => 3, 'name' => 'Emma'],
        ['id' => 4, 'name' => 'Emily'],
        ['id' => 5, 'name' => 'Sofia'],
    ],
];

// Initialize DotNotation with the $array
$dotNotation = new DotNotation($array);

$keys = ['users.2', 'users.3', 'users.4'];

$result = $dotNotation->get($keys);
// The result will be [['id' => 3, 'name' => 'Emma'], ['id' => 4, 'name' => 'Emily'], ['id' => 5, 'name' => 'Sofia']]

// A better view of the result:
// [
//    [0] => ['id' => 3, 'name' => 'Emma'],
//    [1] => ['id' => 4, 'name' => 'Emily'],
//    [2] => ['id' => 5, 'name' => 'Sofia']
// ]

示例 5:使用单个默认值使用get

在这个示例中,我们从数据中检索“user.profile.name”键的值。该键不存在,返回默认值“访客”。

$data = [
            'app'  => [
                'name'    => 'My App',
                'version' => '1.0',
            ],
            'user' => [
                'theme' => 'light',
            ],
        ];

// Initialize DotNotation with your data array
$dotNotation = new DotNotation($data);

// Get the value with a single default value if the key doesn't exist
$username = $dotNotation->get('user.profile.name', 'Guest');

echo "Username: $username"; // Output: Username: Guest

示例 6:使用单个默认值和现有键使用get

在这个示例中,我们从数据中检索“user.profile.pic”键的值。由于该键存在于数据中,返回键的实际值,并忽略默认值。

<?php
$data = [
    'user' => [
        'profile' => [
            'pic' => '625.png',
        ],
    ],
];
// Initialize DotNotation with your data array
$dotNotation = new DotNotation($data);

// Get the value with a single default value if the key exists
$profilePic = $dotNotation->get('user.profile.pic', 'default_pic.png');

echo "Profile Picture: $profilePic"; // Output: Profile Picture: 625.png (value from the data)

示例 7:使用多个默认值使用get

在这个示例中,我们从数据中检索“user.profile.name”和“user.profile.bio”键的值。如果这些键中的任何一个不存在,为每个键返回相应的默认值。

$data = [
    'user' => [
        'profile' => [
            'name' => 'John Doe',
        ],
    ],
];
// Initialize DotNotation with your data array
$dotNotation = new DotNotation($data);

// Get multiple values with a single default value for each key if the key doesn't exist
$profile = $dotNotation->get(['user.profile.name', 'user.profile.bio'], ['Guest', 'No bio available']);

// Output: ['name' => "John Doe", 'Bio' => "No bio available"]

示例 8:使用单个默认值使用多个get

在这个示例中,我们从数据中检索“user.profile.name”和“user.profile.bio”键的值。如果这些键中的任何一个不存在,为每个键返回默认值。

$data = [
    'user' => [
        'profile' => [
            'pic' => '625.png',
        ],
    ],
];
// Initialize DotNotation with your data array
$dotNotation = new DotNotation($data);

// Get multiple values with one default value for each key if the key doesn't exist
$profile = $dotNotation->get(
    ['user.profile.name', 'user.profile.bio'],'No available'
);

// Output: ['name' => "No available", 'Bio' => "No available"]

示例 9:在has方法中使用多个键

在这个示例中,我们检查数据中是否存在“user.profile.name”和“user.profile.bio”键。如果所有键都存在于数据中,则返回true。

$data = [
    'user' => [
        'profile' => [
            'name' => 'John Doe',
        ],
    ],
];
// Initialize DotNotation with your data array
$dotNotation = new DotNotation($data);

// Check if multiple keys exist using an array of keys
$keysToCheck = ['user.profile.name', 'user.profile.bio'];

if ($dotNotation->has($keysToCheck)) {
    echo "All keys exist in the data.";
} else {
    echo "At least one key does not exist in the data.";
}

示例 10:使用hasOne方法

如果所有键中至少有一个存在于数据中,则返回true。

$data = [
    'user' => [
        'profile' => [
            'name' => 'John Doe',
        ],
    ],
];
// Initialize DotNotation with your data array
$dotNotation = new DotNotation($data);

// Check if at least one of the keys exists using an array of keys
$keysToCheck = ['user.profile.name', 'user.profile.bio'];

if ($dotNotation->hasOne($keysToCheck)) {
    echo "At least one key exists in the data.";
} else {
    echo "None of the keys exist in the data.";
}

这些示例展示了Array Dot Notation库在处理使用点表示法访问和操作嵌套数组时的各种场景的高级功能。

贡献

我们欢迎社区贡献以改进和扩展这个库。如果您想贡献,请按照以下步骤操作

  1. 在GitHub上Fork这个仓库。
  2. 将您的Fork克隆到本地。
  3. 为您的功能或错误修复创建一个新的分支。
  4. 进行您的更改,并使用清晰、简洁的提交信息提交它们。
  5. 将您的更改推送到GitHub上的Fork。
  6. 向主仓库提交一个pull request。

报告错误和安全问题

如果您发现本项目中的任何安全漏洞或错误,请通过以下途径通知我们

  • GitHub Issues:您可以在我们的GitHub仓库创建一个问题来报告错误或安全问题。请尽可能提供详细信息,包括复现问题的步骤。

  • 联系:对于敏感的安全相关问题,您可以通过以下联系方式直接联系我们

联系

如果您有任何问题、建议、财务问题,或者想为这个项目做出贡献,请随时联系维护者

我们感谢您的反馈、支持以及任何有助于我们维护和改进这个项目的财务贡献。

许可证

这个库是开源软件,根据MIT许可证授权。