Like Arrays. Only better!
Collections, at their core, are implementations of Iterator, ArrayAccess and Countable. But they're also a lot more than that.
Splice subsections of arrays simply by passing keys:
$collection = new Collection(array('Apple', 'Orange', 'Banana'));
$subset = $collection['0:1'];
// $subset is now: array('Apple', 'Orange')
$collection = new Collection(array('Apple', 'Orange', 'Banana'));
$subset = $collection['-2:2'];
// $subset contains ('Orange', 'Banana')
$collection = new Collection(array('Apple', 'Orange', 'Banana', 'Grapes'));
$subset = $collection[':LAST'];
// $subset is simply array('Grapes')
Quickly and easily Sort
$collection = new Collection(array(100, 50, 70, 10));
$collection->sort(Collection::SORT_ASC);
// $collection's order is now: 10, 50, 70, 100
$collection = new Collection(array(
array(
'name' => 'Sarah',
'job' => 'Manager',
),
array(
'name' => 'Alex',
'job' => 'Developer',
),
array(
'name' => 'Tracy',
'job' => 'HR'
),
));
$collection->sortByMember('name', Collection::SORT_ASC);
For full feature list, check out the docs.
Install via composer:
"require": {
"solution10/collection": "1.*"
}
- PHP >= 5.3
That's it!
See the Github Wiki or the docs/ folder in the repo.
To get api docs; from a checkout of the project run:
$ make && open api/index.html