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 f32fa48291 update compare flags 2018-08-20 22:47:23 +08:00
src update compare flags 2018-08-20 22:47:23 +08:00
stubs update compare flags 2018-08-20 22:47:23 +08:00
tests update compare flags 2018-08-20 22:47:23 +08:00
.gitattributes Add .gitattributes. Move source files. 2018-03-23 22:17:09 +08:00
.gitignore Fix `retainAll()`. Update .gitignore 2018-08-16 20:17:13 +08:00
.travis.yml update travis 2018-08-09 17:40:00 +08:00
LICENSE initial commit 2018-03-20 22:44:25 +08:00
README.md update compare flags 2018-08-20 22:47:23 +08:00
config.m4 Implement ArrayAccess. Fix config.m4, .travis.yml. Update stubs. 2018-03-31 12:02:13 +08:00
config.w32 Update config.w32. Update README. 2018-08-16 01:23:05 +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.

1.1 Notes

  • Requires PHP 7.1 and above.
  • Thread safety:
    • Distinct objects: safe.
    • Shared objects: unsafe.

2. Documentation

2.1 Functionalities

See stubs directory for signature of all classes and methods of this extension, with PHPDoc. They can also serve as IDE helper.

2.2 PHP-style access

The Collection class implements ArrayAccess and Countable interface internally, you can treat an instance of Collection as an ArrayObject.

  • The isset(), unset() keywords can be used on elements of Collection.
  • Elements can be accessed via property and bracket expression.
  • empty(), count() can be used on instance of Collection.
  • Elements can be traversed via foreach() keyword.

2.3 Comparing elements

Some methods of Collection involves comparing two of its elements, which accepts $flags as one of its arguments.

When these methods are being invoked, type of the very first element of the Collection represents that of all other ones. Make sure all elements are of the same type (numeric/string/others), otherwise you're likely to get a segfault.

2.4 Notes

  • The Collection::xxxTo() methods will preserve the original key-value pairs of destination Collection when keys collide.

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 ($value) {
        return $value['sex'] == 'male';
    })
    ->sortedByDescending(function ($value) {
        return $value['age'];
    })
    ->map(function ($value) {
        return $value['name'];
    })
    ->toArray();
// You got $names == ['David', 'Benjamin', 'Bob'].