PHPackages                             artoodetoo/recordset - 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. artoodetoo/recordset

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

artoodetoo/recordset
====================

Work with array as a set of records

v1.2(6y ago)07MITPHPPHP &gt;=7.0

Since Mar 2Pushed 6y ago1 watchersCompare

[ Source](https://github.com/artoodetoo/recordset)[ Packagist](https://packagist.org/packages/artoodetoo/recordset)[ RSS](/packages/artoodetoo-recordset/feed)WikiDiscussions master Synced today

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

Work with array as a set of records.
====================================

[](#work-with-array-as-a-set-of-records)

[![Latest Version on Packagist](https://camo.githubusercontent.com/d2920f0040fd9fcf9d4e2a7f86f81b2f1a9aada2043804cd5653a91df73e6d71/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6172746f6f6465746f6f2f7265636f72647365742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/artoodetoo/recordset)[![Build Status](https://camo.githubusercontent.com/bf59cbf2ec98017ecb5ef21b0b5f2909254774c73995d325c68eae7865e05d64/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6172746f6f6465746f6f2f7265636f72647365742f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/artoodetoo/recordset)[![Quality Score](https://camo.githubusercontent.com/711271fe16d2bd780722425775a7dec5b2f576c2b3d1be02c08965384a6d6642/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6172746f6f6465746f6f2f7265636f72647365742e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/artoodetoo/recordset)[![Total Downloads](https://camo.githubusercontent.com/f72823215e28f4735162b9f030df2992e9572546a26b30ccc3f8dcb9be7b0346/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6172746f6f6465746f6f2f7265636f72647365742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/artoodetoo/recordset)

Installation
------------

[](#installation)

```
composer require artoodetoo/recordset
```

Basic Examples
--------------

[](#basic-examples)

Sort records like SQL does:

```
use R2\Helpers\ArrayRecordset;

$records = [
    ['name' => 'Alfred', 'age' => 40],
    ['name' => 'Mark',   'age' => 40],
    ['name' => 'Lue',    'age' => 45],
    ['name' => 'Ameli',  'age' => 38],
    ['name' => 'Barb',   'age' => 38],
];
$result = (new ArrayRecordset($records))
    ->orderBy('age', 'desc')
    ->orderBy('name', 'asc')
    ->get();

// will be equal to
//    [
//        ['name' => 'Lue',    'age' => 45],
//        ['name' => 'Alfred', 'age' => 40],
//        ['name' => 'Mark',   'age' => 40],
//        ['name' => 'Ameli',  'age' => 38],
//        ['name' => 'Barb',   'age' => 38],
//    ];
```

Get only one column result:

```
$result = (new ArrayRecordset($records))
    ->pluck('name);

// will be equal to
// ['Alfred', 'Mark', 'Lue', 'Ameli', 'Barb']

$result = (new ArrayRecordset($records))
    ->pluck('age', 'name);

// will be equal to
// ['Alfred' => 40, 'Mark' => 40, 'Lue' => 45, 'Ameli' => 38, 'Barb' => 38]
```

Of course you can combine orderBy and pluck. Also you can specify
`CASE_SENSITIVE` and/or `PRESERVE_KEYS`
flags in class constructor. See unit tests for detailed examples.

To group records by some field, call groupBy() method.
`NOTE`: groupBy should be followed by get() or first() or value() call. You cannot sort or group already grouped recordset.

```
$result = (new ArrayRecordset($this->nameAge))
    ->groupBy('age')
    ->get();

// will be equal to
// [
//     38 => [
//         ['name' => 'Ameli',  'age' => 38],
//         ['name' => 'Barb',   'age' => 38],
//     ],
//     40 => [
//         ['name' => 'Alfred', 'age' => 40],
//         ['name' => 'Mark',   'age' => 40],
//     ],
//     45 => [
//         ['name' => 'Lue',    'age' => 45],
//     ],
// ]
```

Advanced examples
-----------------

[](#advanced-examples)

You can use arbitrary comparison function and mix it with others.

```
$comparator = function ($a, $b) {
    return strlen($a['name']) - strlen($b['name']);
};
$result = (new ArrayRecordset($record))
    ->orderBy('age')
    ->orderByCallable($comparator)
    ->get();

// will be equal to
// [
//     ['name' => 'Barb',   'age' => 38],
//     ['name' => 'Ameli',  'age' => 38],
//
//     ['name' => 'Mark',   'age' => 40],
//     ['name' => 'Alfred', 'age' => 40],
//
//     ['name' => 'Lue',    'age' => 45],
// ]
```

You can extend this class by your own macro. Thanks a lot to amazing [spatie trait](https://github.com/artoodetoo/recordset).

```
ArrayRecordset::macro(
    'orderByKeepOnTop',
    function (string $field, string $onTop, string $direction = 'asc') {
        $sign = strtolower($direction) === 'asc' ? 1 : -1;
        $this->comparators[] = function ($a, $b) use ($field, $onTop, $sign) {
            $r = ($b[$field] == $onTop)  ($a[$field] == $onTop);
            return $r == 0 ? strnatcasecmp($a[$field], $b[$field]) * $sign : $r;
        };
        return $this;
    }
);

// Whenever you sort by ascending or descending order,
// Barb will be on top of the list!
$result = (new ArrayRecordset($this->nameAge))
    ->orderByKeepOnTop('name', 'Barb', 'asc')
    ->get();

// will be equal to
// [
//     ['name' => 'Barb',   'age' => 38],
//     ['name' => 'Alfred', 'age' => 40],
//     ['name' => 'Ameli',  'age' => 38],
//     ['name' => 'Lue',    'age' => 45],
//     ['name' => 'Mark',   'age' => 40],
// ]
```

Contributing
------------

[](#contributing)

The project is open-sourced software. Issue reports and PRs are welcome.

License
-------

[](#license)

The project is open-sourced software licensed under the [MIT license](./LICENSE.md).

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 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 ~0 days

Total

3

Last Release

2260d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b982e008c16f8ed2e586e900267b93b01114c28edad839b80b2bad2f4cf1a15d?d=identicon)[artoodetoo](/maintainers/artoodetoo)

---

Top Contributors

[![artoodetoo](https://avatars.githubusercontent.com/u/577710?v=4)](https://github.com/artoodetoo "artoodetoo (7 commits)")

---

Tags

arraysortmacroChainrecordset

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/artoodetoo-recordset/health.svg)

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

###  Alternatives

[doctrine/collections

PHP Doctrine Collections library that adds additional functionality on top of PHP arrays.

6.0k411.1M1.2k](/packages/doctrine-collections)[symfony/property-access

Provides functions to read and write from/to an object or array using a simple string notation

2.8k295.3M2.5k](/packages/symfony-property-access)[nette/utils

🛠 Nette Utils: lightweight utilities for string &amp; array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.

2.1k394.3M1.5k](/packages/nette-utils)[league/config

Define configuration arrays with strict schemas and access values with dot notation

564302.2M24](/packages/league-config)[cuyz/valinor

Dependency free PHP library that helps to map any input into a strongly-typed structure.

1.5k9.2M108](/packages/cuyz-valinor)[openlss/lib-array2xml

Array2XML conversion library credit to lalit.org

31052.5M47](/packages/openlss-lib-array2xml)

PHPackages © 2026

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