PHP code example of remotelyliving / php-collection
1. Go to this page and download the library: Download remotelyliving/php-collection library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
remotelyliving / php-collection example snippets
use RemotelyLiving\PHPCollection\Collection;
$collectionOfStrings = Collection::collect(['foo', 'bar', 'baz']);
$collectionOfNumbers = Collection::collect([1, 2, 3]);
$collectionOfUsers = Collection::collect([$id1 => $user1, $id2 => $user2, $id4 => $user3]);
// we can iterate over collections
foreach ($collectionOfNumbers as $number) {
echo (string) $number;
}
// outputs 1, 2, 3
// we can also perform set operations
$collectionOfUsers->contains($user1); // true
$collectionOfUsers->has($id1); // true
$collectionOfUsers->get($id1, new User()); // defaults to a new User if not found
$collectionOfUsers->some(fn(User $user) => $user->isVerified()); // true if some users are verified
$collectionOfUsers->filter(fn(User $user, $index) => $index > 1)
->map(fn(User $user) => $user->getName())
->first();
$collectionOfNumbers->reverse()
->all();
$collectionOfNumbers->each(fn(int $number, int $index) => $number * $index)
->unique()
->all();
interface CollectionInterface extends \Traversable, \Countable, \IteratorAggregate, \Serializable, \JsonSerializable
{
public function count(): int;
public function map(callable $fn): self;
public function filter(callable $fn): self;
public function each(callable $fn): self;
public function reverse(): self;
/**
* @return mixed|null
*/
public function first();
/**
* @return mixed|null
*/
public function last();
/**
* @param callable $fn
* @param null $initial
*
* @return mixed
*/
public function reduce(callable $fn, $initial = null);
public function unique(): self;
public function diff(Collection $collection): self;
public function merge(Collection $collection): self;
public function union(Collection $collection): self;
public function intersect(Collection $collection): self;
public function sort(callable $comparator = null): self;
public function kSort(callable $comparator = null): self;
public function empty(): bool;
public function all(): array;
public function deferred(): \Generator;
public function values(): array;
public function equals(Collection $collection): bool;
/**
* @param string|int $offset
*
* @return bool
*/
public function has($offset): bool;
/**
* @param mixed $value
*
* @return bool
*/
public function contains($value): bool;
public function some(callable $evaluation): bool;
/**
* @param string|int $offset
* @param mixed|null $default
*
* @return mixed|null
*/
public function get($offset, $default = null);
/**
* @return mixed
*/
public function rand();
/**
* @param string|int ...$offset
*/
public function remove(...$offset): self;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.