PHP code example of temkaa / collections

1. Go to this page and download the library: Download temkaa/collections 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/ */

    

temkaa / collections example snippets


 declare(strict_types = 1);

use Temkaa\Collections\ArrayCollection;
use Temkaa\Collections\Model\Sort;
use Temkaa\Collections\Enum\SortOrder;
use Temkaa\Collections\Filter\AndX;
use Temkaa\Collections\Filter\Greater;
use Temkaa\Collections\Filter\Less;

class SomeClass
{
    public function someArrayFunction(): void
    {
        $products = [
            ['id' => 2, 'name' => 'milk'],
            ['id' => 6, 'name' => 'bread'],
            ['id' => 1, 'name' => 'meat'],
            ['id' => 2, 'name' => 'juice'],
        ];

        var_dump(
            (new ArrayCollection($products))->sort(Sort::path(['name' => SortOrder::Asc, 'id' => SortOrder::Desc]))->toArray(),
            (new ArrayCollection($products))
                ->filter(
                    new AndX(
                        [
                            Greater::path(path: 'id', value: 1),
                            Less::path(path: 'id', value: 10),
                        ]
                    )
                )
                ->toArray(),
        );
    }

    public function someObjectFunction(): void
    {
        $result = Database::all() // Some database query
        
        var_dump(
            (new ArrayCollection($products))->unique(path: 'someField.0.value')->toArray(),
            (new ArrayCollection($products))
                ->map(static fn (object $element): int => $elment->getId())
                ->toArray(),
        );
    }
}

use Temkaa\Collections\ArrayCollection;

$collection = new ArrayCollection([]);
$collection->addElement(value: 'value');
// or
$collection = new ArrayCollection([]);
$collection->addElement(value: 'value', key: 'key');

use Temkaa\Collections\ArrayCollection;

$collection = new ArrayCollection(['element1', 'element2']);
$chunks = $collection->chunk(1);

use Temkaa\Collections\ArrayCollection;

$collection = new ArrayCollection(['element1', 'element2']);
$count = $collection->count();

use Temkaa\Collections\ArrayCollection;

$collection = new ArrayCollection([$object1, $object2]);
$collection->each(static function (object $element): bool {
    if ($element->getId() === 1) {
        return false;
    }
    
    $element->setValue(10);
    
    return true;
});

use Temkaa\Collections\ArrayCollection;
use Temkaa\Collections\Filter\Greater;

$collection = new ArrayCollection([$object1, $object2]);
$newCollection = $collection->filter(Greater::path('property'). value: 10)->toArray();

use Temkaa\Collections\ArrayCollection;

$collection = new ArrayCollection(['element1', 'element2']);
$first = $collection->firstElement();

use Temkaa\Collections\ArrayCollection;

$collection = new ArrayCollection(['element1', 'element2']);
$first = $collection->firstKey();

use Temkaa\Collections\ArrayCollection;

$collection = new ArrayCollection(['element1', 'element2']);
$exists = $collection->hasElement('element1'); // true
// or
$collection = new ArrayCollection(['key' => 'value']);
$exists = $collection->hasElement('value'); // true

use Temkaa\Collections\ArrayCollection;

$collection = new ArrayCollection(['element1', 'element2']);
$exists = $collection->hasKey(0); // true
// or
$collection = new ArrayCollection(['key' => 'value']);
$exists = $collection->hasKey('key'); // true

use Temkaa\Collections\ArrayCollection;

$collection = new ArrayCollection(['element1', 'element2']);
$isEmpty = $collection->empty(); // false

use Temkaa\Collections\ArrayCollection;

$collection = new ArrayCollection(['element1', 'element2']);
$last = $collection->lastElement();

use Temkaa\Collections\ArrayCollection;

$collection = new ArrayCollection(['element1', 'element2']);
$last = $collection->lastKey();

use Temkaa\Collections\ArrayCollection;

$collection = new ArrayCollection(['element1', 'element2']);
$mappedArray = $collection
    ->map(static fn (string $element): string => $element.'1')
    ->toArray(); // ['element11', 'element21']

use Temkaa\Collections\ArrayCollection;

$collection1 = new ArrayCollection(['element1', 'element2']);
$collection2 = new ArrayCollection(['element3', 'element4']);
$resultArray = $collection1
    ->merge($collection2)
    ->toArray(); // ['element1', 'element2', 'element3', 'element4']
// or
$collection1 = new ArrayCollection(['a' => 'element1', 'b' => 'element2']);
$collection2 = new ArrayCollection(['a' => 'element3', 'b' => 'element4']);
$resultArray = $collection1
    ->merge($collection2, recursive: true)
    ->toArray(); // ['a' => ['element1', 'element3'], 'b' => ['element2', 'element4']]

use Temkaa\Collections\ArrayCollection;

$collection = new ArrayCollection(['element1', 'element2']);
$collection->removeElement('element1');
// or
$collection = new ArrayCollection(['a' => 'element1', 'b' => 'element2']);
$collection->removeElement('element1');

use Temkaa\Collections\ArrayCollection;

$collection = new ArrayCollection(['element1', 'element2']);
$collection->removeKey('element1');
// or
$collection = new ArrayCollection(['a' => 'element1', 'b' => 'element2']);
$collection->removeKey('element1');

use Temkaa\Collections\ArrayCollection;

$collection = new ArrayCollection(['element1', 'element2', 'element3']);
$slice = $collection->slice(offset: 1)->toArray(); // ['element2', 'element3']
// or
$collection = new ArrayCollection(['element1', 'element2', 'element3']);
$slice = $collection->slice(offset: 1, length: 1)->toArray(); // ['element2']

use Temkaa\Collections\ArrayCollection;

$collection = new ArrayCollection(['element1', 'element2', 'element3']);
$array = $collection->toArray(); // ['element1', 'element2', 'element3']

use Temkaa\Collections\ArrayCollection;
use Temkaa\Collections\Enum\SortOrder;
use Temkaa\Collections\Model\Sort;

$object1->setId(1);
$object2->setId(2);
$object3->setId(3);

$collection = new ArrayCollection([$object3, $object2, $object1]);
$sorted = $collection
    ->sort(Sort::path(directions: ['id' => SortOrder::DESC]))
    ->toArray(); // [$object3, $object2, $object1]
// or
$collection = new ArrayCollection([1, 2, 3, 4]);
$sorted = $collection
    ->sort(Sort::value(SortOrder::Desc))
    ->toArray(); // [4, 3, 2, 1]

use Temkaa\Collections\ArrayCollection;

$collection = new ArrayCollection([1, 2]);
$unique = $collection->unique()->toArray(); // [1]

$collection = new ArrayCollection([['a' => 1], ['a' => 1]]);
$unique = $collection->unique(path: 'a')->toArray(); // ['a' => 1]