Array manipulation extension for PHP.
This repository has been archived on 2020-03-30. You can view files and clone it, but cannot push or open issues or pull requests.
Go to file
CismonX 55e1bbda22 More tolerant predicate. Add `dropWhile()` and `dropLastWhile()`. 2018-04-11 21:26:51 +08:00
src More tolerant predicate. Add `dropWhile()` and `dropLastWhile()`. 2018-04-11 21:26:51 +08:00
stubs Implement ArrayAccess. Fix config.m4, .travis.yml. Update stubs. 2018-03-31 12:02:13 +08:00
tests More tolerant predicate. Add `dropWhile()` and `dropLastWhile()`. 2018-04-11 21:26:51 +08:00
.gitattributes Add .gitattributes. Move source files. 2018-03-23 22:17:09 +08:00
.gitignore update 2018-03-21 16:21:28 +08:00
.travis.yml Implement ArrayAccess. Fix config.m4, .travis.yml. Update stubs. 2018-03-31 12:02:13 +08:00
LICENSE initial commit 2018-03-20 22:44:25 +08:00
README.md update 2018-03-31 12:16:59 +08:00
config.m4 Implement ArrayAccess. Fix config.m4, .travis.yml. Update stubs. 2018-03-31 12:02:13 +08:00
config.w32 Add .gitattributes. Move source files. 2018-03-23 22:17:09 +08:00

README.md

ext-collections

Travis-CI MIT license

1. Introduction

This PHP extension provides a set of useful functional-style operations on PHP arrays, which makes array manipulation simple and scalable.

Method names and functionalities are inspired by Kotlin.Collections.

2. Documentation

See stubs directory for signature of all classes and methods of this extension, with PHPDoc.

3. Example

Here is a simple example for how to work with arrays gracefully using this extension.

$employees = [
    ['name' => 'Alice', 'sex' => 'female', 'age' => 35],
    ['name' => 'Bob', 'sex' => 'male', 'age' => 29],
    ['name' => 'David', 'sex' => 'male', 'age' => 40],
    ['name' => 'Benjamin', 'sex' => 'male', 'age' => 32]
];
// Trying to get an array of names of male employees,
// sorted by the descending order of their age.
$names = Collection::init($employees)
    ->filter(function ($it) {
        return $it['sex'] == 'male';
    })
    ->sortedByDescending(function ($it) {
        return $it['age'];
    })
    ->map(function ($it) {
        return $it['name'];
    })
    ->toArray();
// You got $names == ['David', 'Benjamin', 'Bob'].