定制 nabeghe/matcher 二次开发

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

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

nabeghe/matcher

最新稳定版本:v1.0.1

Composer 安装命令:

composer require nabeghe/matcher

包简介

A similar alternative to match in PHP for arrays, with a touch of zest.

README 文档

README

A similar alternative to match in PHP for arrays, with a touch of zest.

Extracting values and keys from an array with some variations in functionality.

🫡 Usage

🚀 Installation

You can install the package via composer:

composer require nabeghe/matcher

Example 1: Value

Similar to match in PHP 8.

use \Nabeghe\Matcher\Matcher;

$matched_value = Matcher::value([
    'key1' => 'value 1',
    'key2' => 'value 2',
    'key3' => 'value 3',
    'key4' => 'value 4',
    'key5' => 'value 5',
], 'key5');

echo $matched_value; // value 5

Example 2: Default Value

Similar to match in PHP 8, with a default value in case the key doesn't exist.

use \Nabeghe\Matcher\Matcher;

$matched_value = Matcher::value([
    'key1' => 'value 1',
    'key2' => 'value 2',
    'key3' => 'value 3',
    'key4' => 'value 4',
    'key5' => 'value 5',
], 'key', 'value not found');

echo $matched_value; // value not found

Example 3: Callable Value

Similar to match in PHP 8, with the possibility of the value being callable.

$matched_value = Matcher::value([
    'key1' => 'value 1',
    'key2' => 'value 2',
    'key3' => 'value 3',
    'key4' => 'value 4',
    'key5' => function ($key) {
        return 'value 5';
    },
], 'key5', null, true);

echo $matched_value; // value 5

Example 4: Key

Finding a key based on a value.

use \Nabeghe\Matcher\Matcher;

$matched_value = Matcher::key([
    'key1' => 'value 1',
    'key2' => 'value 2',
    'key3' => 'value 3',
    'key4' => 'value 4',
    'key5' => 'value 5',
], 'value 5');

echo $matched_value; // key5

Example 5: Keys By Value

Finding a set of keys based on a specific value.

use \Nabeghe\Matcher\Matcher;

$matched_value = Matcher::keys([
    'key1' => 'value 1',
    'key2' => 'value 2',
    'key3' => 'value 3',
    'key4' => 'value 4',
    'keys1' => 'value x',
    'keys2' => 'value x',
], 'value x');

print_r($matched_value); // keys1, keys2

Example 6: Keys By Values

Finding a set of keys based on a a set of values.

use \Nabeghe\Matcher\Matcher;

$matched_value = Matcher::keys([
    'key1' => 'value 1',
    'key2' => 'value 2',
    'key3' => 'value 3',
    'key4' => 'value 4',
    'key5' => 'value 5',
], ['value 1', 'value 5']);

print_r($matched_value); // key1, key5

📖 License

Licensed under the MIT license, see LICENSE.md for details.

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-11