PHPackages                             quartet/haydn - 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. quartet/haydn

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

quartet/haydn
=============

set operation library

v1.3.1(10y ago)947.9k[2 issues](https://github.com/quartetcom/haydn/issues)BSD-2-ClausePHPPHP &gt;=5.5

Since May 19Pushed 10y ago4 watchersCompare

[ Source](https://github.com/quartetcom/haydn)[ Packagist](https://packagist.org/packages/quartet/haydn)[ RSS](/packages/quartet-haydn/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (7)Dependencies (1)Versions (10)Used By (0)

Haydn
=====

[](#haydn)

[![Build Status](https://camo.githubusercontent.com/0bd2553c5ad47aa9c10f21298359245e67e0f508ec8da80a4abb605e89e660a9/68747470733a2f2f7472617669732d63692e6f72672f71756172746574636f6d2f686179646e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/quartetcom/haydn)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/3b327f0608b3a43a6d97fbeaaa254c4c15ea2db5fd446fc01b3d4a124f5d0b1f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f71756172746574636f6d2f686179646e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d617374657226733d35633231646432366431313666346134373562333731383965623632653338326466643963356338)](https://scrutinizer-ci.com/g/quartetcom/haydn/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/47d88f200e5c11229d5187c3169b6aa7015f310571717741f10d8217f2774ede/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f71756172746574636f6d2f686179646e2f6261646765732f636f7665726167652e706e673f623d6d617374657226733d66643761663532336535343033353462626337303036626536323866303763306332353162653230)](https://scrutinizer-ci.com/g/quartetcom/haydn/?branch=master)[![Total Downloads](https://camo.githubusercontent.com/caf4a8fc4e0f7fe315b8ce905fae0e99ca0867ab2bd6f7849b328d6a93e449f0/68747470733a2f2f706f7365722e707567782e6f72672f717561727465742f686179646e2f646f776e6c6f6164732e706e67)](https://packagist.org/packages/quartet/haydn)[![Latest Stable Version](https://camo.githubusercontent.com/6987b90e91899ef0448580bc50c9b0b436e9fc2cb70b1fc10454864a2578e10c/68747470733a2f2f706f7365722e707567782e6f72672f717561727465742f686179646e2f762f737461626c652e706e67)](https://packagist.org/packages/quartet/haydn)[![Latest Unstable Version](https://camo.githubusercontent.com/aba4582272bc8a23e9377a9c71bdb6d418bb2220b0afcde06e1c2d40f78446aa/68747470733a2f2f706f7365722e707567782e6f72672f717561727465742f686179646e2f762f756e737461626c652e706e67)](https://packagist.org/packages/quartet/haydn)

[![](https://cloud.githubusercontent.com/assets/89830/7813142/4a65559a-03f3-11e5-8087-4cd0d7e114d2.jpg)](https://cloud.githubusercontent.com/assets/89830/7813142/4a65559a-03f3-11e5-8087-4cd0d7e114d2.jpg)

配列に対してかけ算や列演算を宣言的に指定できるライブラリ。 実装の特徴として、Setに対する各種演算は単に宣言的に行われるのみで、Setオブジェクトを`foreach`等でイテレーションしないかぎり、中身の走査は行われない点。

```
$fruits = [
    ['name' => 'Apple',  'price' => 100],
    ['name' => 'Banana', 'price' =>  80],
];

$drinks = [
    ['name' => 'Yoghurt', 'price' => 200],
    ['name' => 'Soda',    'price' => 120],
    ['name' => 'Spirit',  'price' => 160],
];

$fruitSet = new Set(new ArraySource('fruit', $fruits, new HashKeyColumnMapper()));
$drinkSet = new Set(new ArraySource('drink', $drinks, new HashKeyColumnMapper()));

$fruitDrinkSet = $fruitSet->product($drinkSet);
$result = $fruitDrinkSet->toArray();
```

Install
=======

[](#install)

```
composer require quartet/haydn

```

Usage
=====

[](#usage)

Set declaration (using source object)
-------------------------------------

[](#set-declaration-using-source-object)

```
$fruitsAssoc = [
    ['name' => 'Apple',  'price' => 100],
    ['name' => 'Banana', 'price' =>  80],
];

$fruitsSet = new Set(new ArraySource('fruit', $fruitsAssoc, new HashKeyColumnMapper());

foreach ($fruitSet as $fruit) {
    echo 'name:' . $fruit['name'] . ' price:' . $fruit['price'] . PHP_EOL;
}
```

### Specific Sets

[](#specific-sets)

#### IdenticalSet

[](#identicalset)

```
$fruitsAssoc = [
    ['name' => 'Apple',  'price' => 100],
    ['name' => 'Banana', 'price' =>  80],
];

$fruitsSet = new Set(new ArraySource('fruit', $fruitsAssoc, new HashKeyColumnMapper());

$identicalSet = new IdenticalSet();

$all = $fruitSet->product($identicalSet);

// same as $fruitSet

$all = $identicalSet->product($fruitSet);

// same as $fruitSet

$all = $fruitSet->union($identicalSet);

// same as $fruitSet

$all = $identicalSet->union($fruitSet);

// same as $fruitSet
```

#### EmptySet

[](#emptyset)

```
$fruitsAssoc = [
    ['name' => 'Apple',  'price' => 100],
    ['name' => 'Banana', 'price' =>  80],
];

$fruitsSet = new Set(new ArraySource('fruit', $fruitsAssoc, new HashKeyColumnMapper());

$emptySet = new EmptySet();

$all = $fruitSet->product($emptySet);

// empty

$all = $emptySet->product($fruitSet);

// empty

$all = $fruitSet->union($emptySet);

// same as $fruitSet

$all = $emptySet->union($fruitSet);

// same as $fruitSet
```

Set operations
--------------

[](#set-operations)

### ProductSet

[](#productset)

`Set#product(Set $target)`

2つのSetをかけたSetを作る。（デカルト積）

```
$fruits = [
    ['name' => 'Apple',  'price' => 100],
    ['name' => 'Banana', 'price' =>  80],
];

$drinks = [
    ['name' => 'Yoghurt', 'price' => 200],
    ['name' => 'Soda',    'price' => 120],
    ['name' => 'Spirit',  'price' => 160],
];

$fruitSet = new Set(new ArraySource('fruit', $fruits, new HashKeyColumnMapper()));
$drinkSet = new Set(new ArraySource('drink', $drinks, new HashKeyColumnMapper()));

$fruitDrinkSet = $fruitSet->product($drinkSet);
```

### SelectSet

[](#selectset)

`Set#select(array $selectors)`

Setの各要素に対して、何らかの加工を加えた結果のSet。

- 列の選択
- 列の値の演算 − 複数行への分割

```
$fruits = [
    ['name' => 'Apple',  'price' => 100],
    ['name' => 'Banana', 'price' =>  80],
];

$fruitMenuSet = new Set(new ArraySource('fruit', $fruits, new HashKeyColumnMapper()))
    ->select([function($row) {
        return [
            'menu item name' => $row['name']
        ];
    }]);
;
```

### FilterSet

[](#filterset)

`Set#filter(MatcherInterface $matcher)`

Setの要素に対して、マッチしたものだけを取り出したSet。

```
$products = [
    ['name' => 'Apple',   'price' => 100, 'type' => 'fruit'],
    ['name' => 'Yoghurt', 'price' => 200, 'type' => 'drink'],
    ['name' => 'Soda',    'price' => 120, 'type' => 'drink'],
    ['name' => 'Banana',  'price' =>  80, 'type' => 'fruit'],
    ['name' => 'Spirit',  'price' => 160, 'type' => 'drink'],
];

$fruitSet = new Set(new ArraySource('product', $products, new HashKeyColumnMapper()))
    ->filter(new Matcher(['type' => 'fruit']));
;
```

Matcherは、対象列の名前をキーとして、完全一致する文字列を指定する。 また、完全一致の値ではなく、Closureを渡して動的に評価させることも可能。

```
new Matcher(['type' => function($value) {
    return strpos($value, ':') !== false;
}])
```

### Devide

[](#devide)

`Set#devide(array $matchers)`

Matcherを複数指定して、それぞれのMatcherに対応するSetへ分割する。

```
$products = [
    ['name' => 'Apple',   'price' => 100, 'type' => 'fruit'],
    ['name' => 'Yoghurt', 'price' => 200, 'type' => 'drink'],
    ['name' => 'Soda',    'price' => 120, 'type' => 'drink'],
    ['name' => 'Banana',  'price' =>  80, 'type' => 'fruit'],
    ['name' => 'Spirit',  'price' => 160, 'type' => 'drink'],
];

$productSet = new Set(new ArraySource('product', $products, new HashKeyColumnMapper()));

list($fruitSet, $drinkSet) = $productSet->devide([
    'fruit' => new Matcher(['type' => 'fruit']),
    'drink' => new Matcher(['type' => 'drink']),
]);
```

### Union

[](#union)

`Set#union(Set $target)`

複数のSetを結合して、つながったSetを返す。

```
$fruits = [
    ['name' => 'Apple',  'price' => 100],
    ['name' => 'Banana', 'price' =>  80],
];

$drinks = [
    ['name' => 'Yoghurt', 'price' => 200],
    ['name' => 'Soda',    'price' => 120],
    ['name' => 'Spirit',  'price' => 160],
];

$fruitSet = new Set(new ArraySource('fruit', $fruits, new HashKeyColumnMapper()));
$drinkSet = new Set(new ArraySource('drink', $drinks, new HashKeyColumnMapper()));

$allSet = $fruitSet->union($drinkSet);
```

### Grouping

[](#grouping)

グルーピング演算したSetを返す。ここでグルーピング演算とは、キーとなる集合の各要素それぞれがさらに明細の集合を有するような、「キーの数だけグループを持つ集合」を作成することを指す。 具体的には以下のとおり。

- キー集合Aがあり、
- Aの各要素a1,a2,a3,...ぞれに対して明細集合B1,B2,B3,...を作成したい場合に、
- Aのある要素anを以下のような行に展開し、それをa1,a2,a3,...すべてに適用する
    - ヘッダー行（anグループの先頭を宣言する行）
    - 明細集合Bnの各行
    - フッター行（anグループの末尾を宣言する行）

```
$k1 = new Set(new SingleColumnArraySource('k1', ['あいう', 'かきく']));
$k2 = new Set(new SingleColumnArraySource('k2', ['abc', 'def']));
$k3 = new Set(new SingleColumnArraySource('k3', ['123', '456']));

$g1 = new Set\GroupingSet(
        // Key Set
        $k1->product($k2),
        // Header Generator
        function ($r) { return ['type' => 'header', 'name' => $r['k1'] . '-' . $r['k2']]; },
        // Detail Set Generator
        function ($r) use ($k3) {
            $set = new Set(new SingleRowSource('k1k2', $r));
            $resultSet = $set->product($k3)->select([function ($r) {
                return [
                    'type' => 'detail',
                    'content' => $r['k1'] . ' ' . $r['k2'] . ' ' . $r['k3'],
                ];
            }]);
            return $resultSet;
        },
        // Footer Generator
        null
    );

$all = $g1->toArray();
var_dump($all);

// [
//     'type' => 'header',
//     'name' => 'あいう-abc',
// ], [
//     'type' => 'detail',
//     'content' => 'あいう abc 123',
// ], [
//     'type' => 'detail',
//     'content' => 'あいう abc 456',
// ], [
//     'type' => 'header',
//     'name' => 'あいう-def',
// ], [
//     'type' => 'detail',
//     'content' => 'あいう def 123',
// ], [
//     'type' => 'detail',
//     'content' => 'あいう def 456',
// ], [
//     'type' => 'header',
//     'name' => 'かきく-abc',
// ], [
//     'type' => 'detail',
//     'content' => 'かきく abc 123',
// ], [
//     'type' => 'detail',
//     'content' => 'かきく abc 456',
// ], [
//     'type' => 'header',
//     'name' => 'かきく-def',
// ], [
//     'type' => 'detail',
//     'content' => 'かきく def 123',
// ], [
//     'type' => 'detail',
//     'content' => 'かきく def 456',
// ],
```

Source
------

[](#source)

Setのデータ供給源（データソース）。

- ArraySource
- SingleColumnArraySource
- SingleRowSource

### ArraySource

[](#arraysource)

PHPの配列（2次元）をデータソースとして利用する。

```
$fruitArray = [
    ['id' => 1, 'name' => 'Apple'],
    ['id' => 2, 'name' => 'Banana'],
    ['id' => 3, 'name' => 'Lemon'],
];

$fruitSet = new Set(new ArraySource('fruit', $fruitArray, new HashKeyColumnMapper()));

$output = $fruitSet->toArray();
var_dump($output);

// ['id' => 1, 'name' => 'Apple'],
// ['id' => 2, 'name' => 'Banana'],
// ['id' => 3, 'name' => 'Lemon']
```

### SingleColumnArraySource

[](#singlecolumnarraysource)

1列のみのPHP配列（1次元）をデータソースとして利用する。

```
$fruitArray = [
  'Apple',
  'Banana',
  'Lemon'];

$fruitSet = new Set(new SingleColumnArraySource('fruit', $fruitArray, new NullColumnMapper()));

$output = $fruitSet->toArray();
var_dump($output);

// ['fruit' => 'Apple'],
// ['fruit' => 'Banana'],
// ['fruit' => 'Lemon'],
```

### SingleRowSource

[](#singlerowsource)

1行のみのPHP配列（1次元）をデータソースとして利用する。

```
$fruitArray = ['1','Apple','140'];

$fruitSet = new Set(new SingleRowSource('fruit', $fruitArray, new SimpleArrayColumnMapper([
    'id', 'name', 'price'
])));

$output = $fruitSet->toArray();
var_dump($output);

// ['id'=>1, 'name' => 'Apple', 'price'=>140],
```

ColumnMapper
------------

[](#columnmapper)

Sourceの列名マッピング

- HashKeyColumnMapper
- SimpleArrayColumnMapper
- NullColumnMapper
- ChainMapper

### HashKeyColumnMapper

[](#hashkeycolumnmapper)

各行が連想配列になっているデータソースで、連想配列のキー名をそのまま列名として使う。

```
$fruitArray = [
    ['id' => 1, 'name' => 'Apple'],
    ['id' => 2, 'name' => 'Banana'],
    ['id' => 3, 'name' => 'Lemon'],
];

$fruitSet = new Set(new ArraySource('fruit', $fruitArray, new HashKeyColumnMapper()));

$output = $fruitSet->toArray();
var_dump($output);

// ['id' => 1, 'name' => 'Apple'],
// ['id' => 2, 'name' => 'Banana'],
// ['id' => 3, 'name' => 'Lemon']
```

### SimpleArrayColumnMapper

[](#simplearraycolumnmapper)

キーの無い配列データソースに、列名を配列で与える。

```
$fruitColumn = ['id', 'name'];

$fruitArray = [
    [1, 'Apple'],
    [2, 'Banana'],
    [3, 'Lemon'],
];

$fruitSet = new Set(new ArraySource('fruit', $fruitArray, new SimpleArrayColumnMapper($fruitColumn)));

$output = $fruitSet->toArray();
var_dump($output);

// ['id' => 1, 'name' => 'Apple'],
// ['id' => 2, 'name' => 'Banana'],
// ['id' => 3, 'name' => 'Lemon']
```

### NullColumnMapper

[](#nullcolumnmapper)

列名マップを使わない。

```
$fruitArray = [
    [1, 'Apple'],
    [2, 'Banana'],
    [3, 'Lemon'],
];

$fruitSet = new Set(new ArraySource('fruit', $fruitArray, new NullColumnMapper()));

$output = $fruitSet->toArray();
var_dump($output);

// [0 => 1, 1 => 'Apple'],
// [0 => 2, 1 => 'Banana'],
// [0 => 3, 1 => 'Lemon']
```

### ChainMapper

[](#chainmapper)

列名マッパーを複数チェインさせる。

Support
=======

[](#support)

If you find a bug or have a question, or want to request a feature, create an issue or pull request for it on [Issues](https://github.com/quartetcom/haydn/issues).

Copyright
=========

[](#copyright)

Copyright (c) 2015 GOTO Hidenori, All rights reserved.

License
=======

[](#license)

[The BSD 2-Clause License](http://opensource.org/licenses/BSD-2-Clause)

###  Health Score

33

—

LowBetter than 73% of packages

Maintenance16

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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

Every ~22 days

Recently: every ~31 days

Total

8

Last Release

3884d ago

PHP version history (2 changes)v1.0.0PHP ~5.5

v1.2.0PHP &gt;=5.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/6a68a4dab8a48ed7cea7379e065bbe9cbb2c68a6718c9d678afa8e0d261f1255?d=identicon)[hidenorigoto](/maintainers/hidenorigoto)

---

Top Contributors

[![hidenorigoto](https://avatars.githubusercontent.com/u/89830?v=4)](https://github.com/hidenorigoto "hidenorigoto (103 commits)")

---

Tags

set

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/quartet-haydn/health.svg)

```
[![Health](https://phpackages.com/badges/quartet-haydn/health.svg)](https://phpackages.com/packages/quartet-haydn)
```

###  Alternatives

[phpcollection/phpcollection

General-Purpose Collection Library for PHP

96764.5M34](/packages/phpcollection-phpcollection)[marc-mabe/php-enum

Simple and fast implementation of enumerations with native PHP

49753.5M104](/packages/marc-mabe-php-enum)[kartik-v/yii2-tree-manager

An enhanced tree management module with tree node selection and manipulation using nested sets.

151545.0k15](/packages/kartik-v-yii2-tree-manager)[chdemko/sorted-collections

Sorted Collections for PHP &gt;= 8.4

222.6M3](/packages/chdemko-sorted-collections)[somnambulist/collection

Collection implementation with no dependencies.

1157.1k9](/packages/somnambulist-collection)[antares/accessible

PHP library that allows you to define your class' getters, setters and constructor with docblock annotations.

123.9k1](/packages/antares-accessible)

PHPackages © 2026

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