PHPackages                             yuloh/pattern-recognition - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. yuloh/pattern-recognition

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

yuloh/pattern-recognition
=========================

A pattern matching library for arrays

v0.1.0(10y ago)422[1 issues](https://github.com/matt-allan/pattern-recognition/issues)MITPHPPHP &gt;=5.4.0

Since Mar 28Pushed 10y agoCompare

[ Source](https://github.com/matt-allan/pattern-recognition)[ Packagist](https://packagist.org/packages/yuloh/pattern-recognition)[ RSS](/packages/yuloh-pattern-recognition/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (1)Versions (2)Used By (0)

Pattern Recognition
===================

[](#pattern-recognition)

A pattern matcher for PHP arrays.

Need to pick out an array based on a subset of its key value pairs? Say you've got:

```
['x' => 1]            -> A
['x' => 1, 'y' => 1]  -> B
['x' => 1, 'y' => 2 ] -> C
```

Then this library would give you:

```
['x' => 1]           -> A
['x' => 2]           -> no match
['x' => 1, 'y' => 1] -> B
['x' => 1, 'y' => 2] -> C
['x' => 2, 'y' => 2] -> no match
['y' => 1]           -> no match
```

Quick Example
-------------

[](#quick-example)

```
$pm = new Yuloh\PatternRecognition\Matcher;

$pm
    ->add(['a' => 1], 'A')
    ->add(['b' => 2], 'B');

$pm->find(['a' => 1]); // returns 'A'
$pm->find(['a' => 2]); // returns null
$pm->find(['a' => 1, 'b' => 1]); // returns 'A'. 'b' => 1 is ignored, it was never registered.
```

Since you are matching a subset, the pattern to find can contain any number of extra key value pairs.

Install
-------

[](#install)

```
composer require yuloh/pattern-recognition
```

Why
---

[](#why)

Since this library is a port of the javascript library patrun, [the rationale](https://github.com/rjrodger/patrun#the-why) is the same. This library lets you build a simple decision tree to avoid writing if statements.

Rules
-----

[](#rules)

1. More specific matches beat less specific matches. That is, more key value pairs beat fewer.
2. Array keys are checked in alphabetical order.
3. Exact matches are more specific than globs.
4. Matches are more specific than root matches.

```
$pm = (new Matcher())
    ->add(['a' => 0], 'A')
    ->add(['c' => 2], 'C')
    ->add(['a' => 0, 'b' => 1], 'AB')
    ->(['a' => '*'], 'AG')
    ->add([], 'R');

$pm->find(['a' => 0, 'b' => 1]); // 'AB', because more specific matches beat less specific matches.
$pm->find(['a' => 0, 'c' => 2]); // 'A', because a comes before c and keys are checked in alphabetical order.
$pm->find(['a' => 0]); // 'A' as exact match, because exact matches are more specific than globs.
$pm->find(['a' => 2]); // 'AG' as glob match, as matches are more specific than root matches.
$pm->find(['b' => 2]); // 'R', as root match.
```

API
---

[](#api)

```
add(array $pattern, mixed $data)

```

Register a pattern, and the data that will be returned if an input matches. Both keys and values are considered to be strings. Other types are converted to strings.

```
remove(array $pattern)

```

Remove this pattern, and it's data, from the matcher.

```
find(array $pattern)

```

Find the given pattern and return it's data.

```
jsonSerialize()

```

Serializes the object to a value that can be serialized natively by json\_encode().

```
toArray()

```

Serializes the matcher to an array of the registered matches and data.

###  Health Score

22

—

LowBetter than 21% of packages

Maintenance15

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3746d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/9440455?v=4)[Matt A](/maintainers/matt-allan)[@matt-allan](https://github.com/matt-allan)

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/yuloh-pattern-recognition/health.svg)

```
[![Health](https://phpackages.com/badges/yuloh-pattern-recognition/health.svg)](https://phpackages.com/packages/yuloh-pattern-recognition)
```

###  Alternatives

[outl1ne/nova-color-field

A Laravel Nova Color Picker field.

26263.8k](/packages/outl1ne-nova-color-field)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
