1. Go to this page and download the library: Download nalabdou/algebra-php 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/ */
Algebra::from($input) // array, generator, Traversable → RelationalCollection
Algebra::pipe($input, $fn) // build + execute in one expression
Algebra::parallel(['a' => $c1, 'b' => $c2]) // concurrent via PHP Fibers
// INNER JOIN — unmatched left rows dropped — O(n+m) hash index
->innerJoin($right, leftKey: 'userId', rightKey: 'id', as: 'owner')
// LEFT JOIN — unmatched left rows kept with null
->leftJoin($right, on: 'userId=id', as: 'owner')
// SEMI JOIN — existence check, no right data attached
->semiJoin($right, leftKey: 'id', rightKey: 'orderId')
// ANTI JOIN — rows with no match on right
->antiJoin($right, leftKey: 'id', rightKey: 'orderId')
// CROSS JOIN — cartesian product (all left × all right)
->crossJoin($right, leftPrefix: 'size_', rightPrefix: 'colour_')
// ZIP — positional merge (index 0 with 0, 1 with 1, …)
->zip($right, leftAs: 'label', rightAs: 'value')
->intersect($right, by: 'productId') // A ∩ B — rows in both
->except($right, by: 'id') // A − B — rows only in left
->union($right, by: 'email') // A ∪ B — merged, deduplicated
->symmetricDiff($right, by: 'id') // A △ B — rows in one but not both
// WHERE — string expression
->where("item['status'] == 'paid' and item['amount'] > 100")
->where("contains(item['email'], '@company.com')")
->where("length(item['name']) > 3")
// WHERE — closure (zero overhead, full PHP)
->where(fn($r) => $r['status'] === 'paid' && $r['amount'] > 100)
// SELECT — project each row
->select('id') // pluck single field
->select(fn($r) => ['id' => $r['id'],
'name' => strtoupper($r['name'])])
use Nalabdou\Algebra\Contract\AggregateInterface;
final class GeomeanAggregate implements AggregateInterface
{
public function name(): string { return 'geomean'; }
public function compute(array $values): float|null
{
if (empty($values)) { return null; }
$product = array_product(array_map('abs', $values));
return $product ** (1 / count($values));
}
}
// Register once at bootstrap
Algebra::aggregates()->register(new GeomeanAggregate());
// Use anywhere
Algebra::from($data)
->groupBy('category')
->aggregate(['geo' => 'geomean(price)'])
->toArray();
use Nalabdou\Algebra\Contract\AdapterInterface;
final class DoctrineCollectionAdapter implements AdapterInterface
{
public function supports(mixed $input): bool
{
return $input instanceof \Doctrine\Common\Collections\Collection;
}
public function toArray(mixed $input): array
{
return array_values($input->toArray());
}
}