PHPackages                             ginq/ginq - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ginq/ginq

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ginq/ginq
=========

LINQ to Object inspired DSL for PHP

v0.2.4(10y ago)192257.5k↑12.5%19[6 issues](https://github.com/akanehara/ginq/issues)3MITPHPPHP &gt;=5.3.0

Since Sep 17Pushed 3y ago23 watchersCompare

[ Source](https://github.com/akanehara/ginq)[ Packagist](https://packagist.org/packages/ginq/ginq)[ RSS](/packages/ginq-ginq/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (15)Used By (3)

Ginq
====

[](#ginq)

### Array handling in PHP? Be happy with Ginq!

[](#array-handling-in-php-be-happy-with-ginq)

**Ginq** is a **DSL** that can handle arrays and iterators of PHP unified.

**Ginq** is inspired by **Linq to Object**, but is not a clone.

Many functions in **Ginq** are evaluated in lazy, and no actions are taken until that time. This features bring you many benefits.

Install
=======

[](#install)

composer.json:

```
{
    "require": {
        "ginq/ginq": "dev-master"
    }
}
```

see:

Usage
=====

[](#usage)

```
$xs = Ginq::from(array(1,2,3,4,5,6,7,8,9,10))
        ->where(function($x) { return $x % 2 != 0; })
        ->select(function($x) { return $x * $x; });
        ;
```

You pass **Ginq** data and build a query with it. In this example above, you order **Ginq** to choose even numbers and square each of them.

But **Ginq** do nothing, **Ginq** knows only you want a result of chosen and squared numbers.

Let's execute `foreach` loop with **Ginq** to get the result.

```
foreach ($xs as $x) { echo "$x "; }
```

The result is

```
1 9 25 49 81

```

You got the expected result!

Next, you can get an array with `toList`.

```
$xs->toList();
```

```
array(1,9,25,49,81);

```

**Ginq** has functions, well-known in SQL, such as `join()`, `orderBy()`, and `groupBy()` other than `select()`, `where()` listed above.

Selector and Predicate
----------------------

[](#selector-and-predicate)

Most of methods in **Ginq** receive a closure as a argument.

You may not be familiar with closures, but it is very simple things. There are just three types of closures in **Ginq**, you can remember simply. These are predicate, selector, and connection selector.

### Predicate

[](#predicate)

A closure that passed to a method that do select, such as `where()` is called **predicate**.

Predicate is a closure that receive a pair of key and values in the elements and return boolean value.

```
function ($v, [$k]) { return $v % 2 == 0; }
```

You get even numbers when you pass this closure to `where()`. You can skip second argument when you don't need it in the process.

### Selector

[](#selector)

A closure that passed to a method that do projection, such as `select()` is called **selector**.

Selector is a closure that receive a pair of key and value in the elements and create a new value or key, and then return it.

```
function ($v, [$k]) { return $v * $v ; }
```

You get squared numbers of original when you pass this closure to `select()`.

This function is used to specify the key of grouping with `groupBy()`, the key of sorting with `groupBy()`.

### Connection Selector

[](#connection-selector)

**Connection Selector** is one of the selector that combine two elements into one, is used with `join()`, `zip()`.

```
function ($v0, $v1, [$k0, $k1]) { return array($v0, $v1) ; }
```

This function receive 4 arguments, two values and two keys, and then create new value or key and return it. You can skip arguments when you don't need it in the process.

These are `zip()` example that combine each elements from two arrays.

```
$foods  = array("meat", "pasta", "salada");
$spices = array("time", "basil", "dill");

$xs = Ginq::from($foods)
        ->zip($spices, function($f, $s) {
            return "$f with $s!";
        })
        ;

foreach ($xs as $x) { echo "$x\n"; }
```

```
meat with time!
pasta with basil!
salada with dill!

```

Shortcuts of predicate and selector
-----------------------------------

[](#shortcuts-of-predicate-and-selector)

**Selector** can receive a character string instead of a closure.

They return the value of the field when the element is an object, or return the value of the key when it is an array.

So,

```
Ginq::from($xs)->select('[key].property');
```

The example above is same as two examples below.

```
Ginq::from($xs)->select(
    function ($v, $k) { return $v['key'].property; }
);
```

see: Property Access (Symfony) [http://symfony.com/doc/current/components/property\_access/index.html](http://symfony.com/doc/current/components/property_access/index.html)

More complex examples
---------------------

[](#more-complex-examples)

References
----------

[](#references)

Development
===========

[](#development)

PreRequirements
---------------

[](#prerequirements)

- `Docker` installed.
- `Docker Compose` installed.

How to start development (Run test)
-----------------------------------

[](#how-to-start-development-run-test)

- cd docker
- docker-compose up -d
- docker-compose exec php ash
- (in php container)
    - composer install
    - vendor/bin/phpunit

Note
----

[](#note)

- You can use [illuminate/support](https://github.com/illuminate/support) library. (Laravel `Collection`, ...)

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity49

Moderate usage in the ecosystem

Community31

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 74.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~74 days

Recently: every ~117 days

Total

10

Last Release

3953d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/36f36b8e6823b561d2d8143f43c0ce29270d43103ff9aad8ad6dfd6f3d6b9a67?d=identicon)[akanehara](/maintainers/akanehara)

---

Top Contributors

[![akanehara](https://avatars.githubusercontent.com/u/1199679?v=4)](https://github.com/akanehara "akanehara (175 commits)")[![tanakahisateru](https://avatars.githubusercontent.com/u/403893?v=4)](https://github.com/tanakahisateru "tanakahisateru (28 commits)")[![amkt922](https://avatars.githubusercontent.com/u/3191521?v=4)](https://github.com/amkt922 "amkt922 (8 commits)")[![ritalin](https://avatars.githubusercontent.com/u/908072?v=4)](https://github.com/ritalin "ritalin (5 commits)")[![hason](https://avatars.githubusercontent.com/u/288535?v=4)](https://github.com/hason "hason (5 commits)")[![sukobuto](https://avatars.githubusercontent.com/u/1277008?v=4)](https://github.com/sukobuto "sukobuto (3 commits)")[![sogaoh](https://avatars.githubusercontent.com/u/2090887?v=4)](https://github.com/sogaoh "sogaoh (3 commits)")[![vain0x](https://avatars.githubusercontent.com/u/6957032?v=4)](https://github.com/vain0x "vain0x (2 commits)")[![okinaka](https://avatars.githubusercontent.com/u/120861?v=4)](https://github.com/okinaka "okinaka (1 commits)")[![holyshared](https://avatars.githubusercontent.com/u/167190?v=4)](https://github.com/holyshared "holyshared (1 commits)")[![gunjiro](https://avatars.githubusercontent.com/u/556898?v=4)](https://github.com/gunjiro "gunjiro (1 commits)")[![emonkak](https://avatars.githubusercontent.com/u/188621?v=4)](https://github.com/emonkak "emonkak (1 commits)")[![upsilon](https://avatars.githubusercontent.com/u/92577?v=4)](https://github.com/upsilon "upsilon (1 commits)")[![bryant1410](https://avatars.githubusercontent.com/u/3905501?v=4)](https://github.com/bryant1410 "bryant1410 (1 commits)")

---

Tags

arrayiteratorDSLlinq

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ginq-ginq/health.svg)

```
[![Health](https://phpackages.com/badges/ginq-ginq/health.svg)](https://phpackages.com/packages/ginq-ginq)
```

###  Alternatives

[athari/yalinqo

YaLinqo, a LINQ-to-objects library for PHP

4561.2M5](/packages/athari-yalinqo)[winzou/state-machine

A very lightweight yet powerful PHP state machine

52113.7M18](/packages/winzou-state-machine)[malarzm/collections

Various implementations of Doctrine's Collection interface

2368.1k](/packages/malarzm-collections)[ayesh/case-insensitive-array

Class to store and access data in a case-insensitive fashion, while maintaining the integrity and functionality of a regular array.

1020.7k3](/packages/ayesh-case-insensitive-array)[chdemko/bitarray

BitArray for PHP &gt;= 8.2

1116.2k](/packages/chdemko-bitarray)[rotexsoft/versatile-collections

A collection package that can be extended to implement things such as a Dependency Injection Container, RecordSet objects for housing database records, a bag of http cookies, or technically any collection of items that can be looped over and whose items can each be accessed using array-access syntax or object property syntax.

186.0k1](/packages/rotexsoft-versatile-collections)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
