Simple library for manipulating array or object as collection.
ℹ Supported dot-notation and asterisks rules.
The purpose of this library is to provide a generic class for interacting with arrays of data. Convenient support for dot notation and asterisk keys.
$ composer require chipslays/collection
Collection can created by helper function
collection()
.
Get item from collection by using dot notation.
use Chipslays\Collection\Collection;
$collection = new Collection([
'user' => [
'name' => 'chipslays',
],
]);
$name = $collection->get('user.name'); // chipslays
$email = $collection->get('user.email', 'default@email.com'); // default@email.com
$array = $collection->user; // array('name' => 'chipslays')
$name = $collection->user['name']; // chipslays
$collection = collection([
'foo' => [
'bar' => ['baz' => 1],
'bam' => ['baz' => 2],
'boo' => ['baz' => 3],
],
]);
$collection->get('foo.*.baz');
// Array
// (
// [0] => 1
// [1] => 2
// [2] => 3
// )
$collection->get('foo.*');
// Array
// (
// [0] => Array
// (
// [baz] => 1
// )
// [1] => Array
// (
// [baz] => 2
// )
// [2] => Array
// (
// [baz] => 3
// )
// )
$collection = collection([
'foo' => [
'bar' => ['baz' => 1],
],
]);
$collection->get('foo.*.baz');
// 1
$collection->get('foo.*');
// Array
// (
// [baz] => 1
// )
Set/overwrite item in collection using by dot notation key.
use Chipslays\Collection\Collection;
$collection = new Collection([
'user' => [
'name' => 'chipslays',
],
]);
$collection->set('user.name', 'john doe');
$collection->set('user.email', 'john.doe@email.com');
$name = $collection->get('user.name'); // john doe
$email = $collection->get('user.email'); // john.doe@email.com
Check exists item in collection using by dot notation key.
use Chipslays\Collection\Collection;
$collection = new Collection([
'user' => [
'name' => 'chipslays',
],
]);
$hasName = $collection->has('user.name'); // true
$hasEmail = $collection->has('user.email'); // false
Returns first item from collection.
use Chipslays\Collection\Collection;
$collection = new Collection(['foo', 'bar', 'baz']);
echo $collection->first(); // foo
Returns last item from collection.
use Chipslays\Collection\Collection;
$collection = new Collection(['foo', 'bar', 'baz']);
echo $collection->last(); // baz
Getting first item and remove her from collection.
use Chipslays\Collection\Collection;
$collection = new Collection(['foo', 'bar', 'baz']);
echo $collection->shift(); // foo
echo $collection->count(); // 2
Returns values without keys as collection.
use Chipslays\Collection\Collection;
$collection = new Collection(['color' => 'green', 'name' => 'apple']);
print_r($collection->values()); // collection(green, apple)
Returns keys without values as collection.
use Chipslays\Collection\Collection;
$collection = new Collection(['color' => 'green', 'name' => 'apple']);
print_r($collection->keys()); // collection(color, name)
Returns only selected keys.
use Chipslays\Collection\Collection;
$collection = new Collection(['color' => 'green', 'name' => 'apple']);
print_r($collection->only(['color'])); // collection(color => green)
Get count of items in collection.
use Chipslays\Collection\Collection;
$collection = new Collection(range(1, 10));
echo $collection->count(); // 10
echo count($collection); // 10
Clear all items in collection.
use Chipslays\Collection\Collection;
$collection = new Collection(range(1, 10));
$collection->clear();
Get collection items as array.
use Chipslays\Collection\Collection;
$collection = new Collection(range(1, 10));
$collection->toArray();
Get collection items as object (stdClass).
use Chipslays\Collection\Collection;
$collection = new Collection(range(1, 10));
$collection->toObject();
Get items as printable string.
use Chipslays\Collection\Collection;
$collection = new Collection(['one', 'two']);
echo (string) $collection;
/** output string */
Array
(
[0] => one
[1] => two
)
chipslays/array
- Simple library for array manipulate.
MIT