PHPackages                             penobit/linq - 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. penobit/linq

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

penobit/linq
============

Language-Integrated Query (LINQ) for PHP language.

v1.4(4y ago)2142MITPHPPHP &gt;=5.6

Since Jul 12Pushed 4y ago1 watchersCompare

[ Source](https://github.com/penobit/linq)[ Packagist](https://packagist.org/packages/penobit/linq)[ Docs](https://github.com/penobit/linq)[ RSS](/packages/penobit-linq/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (3)Versions (6)Used By (0)

LinQ
====

[](#linq)

Language-Integrated Query (**LINQ**) for **PHP** language
---------------------------------------------------------

[](#language-integrated-query-linq-for-php-language)

Installation
------------

[](#installation)

```
composer require penobit/linq
```

Usage
-----

[](#usage)

You can start using this package right away by importing your JSON data from a file:

```
use Penobit\Linq\Linq;
$linq = new Linq($data);
```

Importing data from a PHP array or json string:

```
$linq->collect(['id'=>1, 'name'=>'Penobit']);
```

Or using short handed function

```
linq($data);
```

Example JSON Data
-----------------

[](#example-json-data)

Let's have a quick example:

```
//data.json
{
   "name": "Penobit Linq for PHP",
   "description": "This is an example for php linq by Penobit.com",
   "vendor":{
      "name": "Penobit",
      "email": "info@penobit.com",
      "website":"www.penobit.com"
   },
   "users":[
      {"id":1, "name":"R8", "location": "Tehran"},
      {"id":2, "name":"Amin Goodarzi", "location": "Ohio"},
      {"id":3, "name":"Amir Jahangiri", "location": "Louisiana"},
      {"id":4, "name":"Jack Smith", "location": "New York"},
      {"id":5, "name":"John Doe", "location": "Manchester"},
      {"id":6, "name":"Sara Savari", "location": "Tehran", "visits": [
         {"name": "Home page", "year": 2020},
         {"name": "Client area", "year": 2021},
         {"name": "Admin panel", "year": 2019}
      ]}
   ],
   "products": [
      {"id":1, "userID": 2, "city": "TEH", "name":"iPhone", "cat":1, "price": 80000},
      {"id":2, "userID": 2, "city": null, "name":"macbook pro", "cat": 2, "price": 150000},
      {"id":3, "userID": 2, "city": "NY", "name":"iWatch", "cat": 1, "price": 12000},
      {"id":4, "userID": 1, "city": null, "name":"iMac", "cat":1, "price": 15000},
      {"id":5, "userID": 1, "city": "TEH", "name":"macbook air", "cat": 2, "price": 110000},
      {"id":6, "userID": 2, "city": null, "name":"macbook air 1", "cat": 2, "price": 81000}
   ]
}
```

```
use Penobit\JsonQ\Linq;

$q = new Linq($data);
$res = $q->from('products')
    ->where('cat', '=', 2)
    ->get('name', 'price');

// or you could use $q->select('name', 'price') or $q->fetch('name', 'price')  instead of getch

var_dump($res->toArray());

//This will print
/*
array:3 [▼
  1 => {#7 ▼
    +"name": "macbook pro"
    +"price": 150000
  }
  4 => {#8 ▼
    +"name": "macbook air"
    +"price": 110000
  }
  5 => {#9 ▼
    +"name": "macbook air 1"
    +"price": 81000
  }
]
*/
```

Let's say we want to get the Summation of *price* of the Queried result. We can do it easily by calling the **sum()** method instead of **get()**:

```
$result = $linq->from('products')
        ->where('cat', '=', 2)
        ->sum('price');

var_dump($result);

//It will print:
/*
365000
*/
```

That was easy, wasn't it?

API
---

[](#api)

> Here is the list of available methods for querying.

Following API examples are shown based on the sample JSON data given [here](#example-json-data)

**List of API:**

- [LinQ](#linq)
    - [Language-Integrated Query (**LINQ**) for **PHP** language](#language-integrated-query-linq-for-php-language)
    - [Installation](#installation)
    - [Usage](#usage)
    - [Example JSON Data](#example-json-data)
    - [API](#api)
        - [`fetch()`](#fetch)
        - [`find(path)`](#findpath)
        - [`from(path)`](#frompath)
        - [`at(path)`](#atpath)
        - [`where(key, condition, val)`](#wherekey-condition-val)
        - [`where(key, op, val)`](#wherekey-op-val)
        - [`callableWhere(fn)`](#callablewherefn)
        - [`orCallableWhere(fn)`](#orcallablewherefn)
        - [`orWhere(key, op, val)`](#orwherekey-op-val)
        - [`whereIn(key, val)`](#whereinkey-val)
        - [`whereNotIn(key, val)`](#wherenotinkey-val)
        - [`whereNull(key)`](#wherenullkey)
        - [`whereNotNull(key)`](#wherenotnullkey)
        - [`whereStartsWith(key, val)`](#wherestartswithkey-val)
        - [`whereEndsWith(key, val)`](#whereendswithkey-val)
        - [`whereContains(key, val)`](#wherecontainskey-val)
        - [`sum(column)`](#sumcolumn)
        - [`count()`](#count)
        - [`size()`](#size)
        - [`max(column)`](#maxcolumn)
        - [`min(column)`](#mincolumn)
        - [`avg(column)`](#avgcolumn)
        - [`first()`](#first)
        - [`last()`](#last)
        - [`nth(index)`](#nthindex)
        - [`exists()`](#exists)
        - [`groupBy(column)`](#groupbycolumn)
        - [`sort(order)`](#sortorder)
        - [`sortBy(column, order)`](#sortbycolumn-order)
        - [`reset(data)`](#resetdata)
        - [`copy()`](#copy)
    - [Example Files](#example-files)

### `fetch()`

[](#fetch)

This method will execute queries and will return the resulted data. You need to call it finally after using some query methods. Details can be found in other API examples like `get()`.

### `find(path)`

[](#findpath)

- `path` -- the path hierarchy of the data you want to find.

You don't need to call `fetch()` method after this. Because this method will fetch and return the data by itself.

**caveat:** You can't chain further query methods after it. If you need that, you should use `at()` or `from()` method.

**example:**

Let's say you want to get the value of *'cities'* property of your Json Data. You can do it like this:

```
$q = new Linq($data);
echo $q->find('vendor.name');
```

If you want to traverse to more deep in hierarchy, you can do it like:

```
$q = new Linq($data);
echo $q->find('vendor.name');
```

### `from(path)`

[](#frompath)

- `path` (optional) -- the path hierarchy of the data you want to start query from.

By default, query would be started from the root of the JSON Data you've given. If you want to first move to a nested path hierarchy of the data from where you want to start your query, you would use this method. Skipping the `path` parameter or giving **'.'** as parameter will also start query from the root Data.

Difference between this method and `find()` is that, `find()` method will return the data from the given path hierarchy. On the other hand, this method will return the Object instance, so that you can further chain query methods after it.

**example:**

Let's say you want to start query over the values of *'vendor.name'* property of your JSON Data. You can do it like this:

```
$q = new Linq($data);
echo $q->from('vendor.name')->get();
```

If you want to traverse to more deep in hierarchy, you can do it like:

```
$q = new Linq($data);
echo $q->from('users.5.visits')->get();
```

### `at(path)`

[](#atpath)

This is an alias method of `from()` and will behave exactly like that.

### `where(key, condition, val)`

[](#wherekey-condition-val)

- `key` -- the property name of the data. Or you can pass a Function here to group multiple query inside it. See details in [example](examples/where.js)
- `val` -- value to be matched with. It can be a *int*, *string*, *bool* or even *Function* - depending on the `op`.
- `op` -- operand to be used for matching. The following operands are available to use:
- `=` : For weak equality matching
- `eq` : Same as `=`
- `!=` : For weak not equality matching
- `neq` : Same as `!=`
- `==` : For strict equality matching
- `seq` : Same as `==`
- `!==` : For strict not equality matching
- `sneq` : Same as `!==`
- `>` : Check if value of given **key** in data is Greater than **val**
- `gt` : Same as `>`
- `=`
- `from('users')
    ->where('id', '=', 1)
    ->where('location', '=', 'Tehran')
    ->get();
```

### `callableWhere(fn)`

[](#callablewherefn)

Parameter of `callableWhere()` is callable function that should return a `bool` (true/false)

```
$q = new Linq($data);
$res = $q->from('users')
    ->callableWhere(function($item) {
        return $item['price'] > 3000;
    })
    ->toArray();
```

### `orCallableWhere(fn)`

[](#orcallablewherefn)

Parameter and behaviour of `orCallableWhere()` is the same as `callableWhere()`

```
$res = $q->from('users')
    ->where('id', 1)
    ->callableWhere(function($item) {
        return $item['location'] == 'Tehran' &&  substr($item['name'], 0, 3) == 'mac';
    })
    ->orCallableWhere(function($item) {
        return $item['price'] > 3000;
    })
    ->toArray();
```

### `orWhere(key, op, val)`

[](#orwherekey-op-val)

Parameters of `orWhere()` are the same as `where()`. The only difference between `where()` and `orWhere()` is: condition given by the `orWhere()` method will OR-ed the result with other conditions.

For example, if you want to find the users with *id* of `1` or `2`, you can do it like this:

```
$q = new Linq($data);
$res = $q->from('users')
    ->where('id', '=', 1)
    ->orWhere('id', '=', 2)
    ->get();
// Or
$res = $q->from('users')
    ->where('id', 1)
    ->orWhere('id', 2)
    ->get();
```

### `whereIn(key, val)`

[](#whereinkey-val)

- `key` -- the property name of the data
- `val` -- it should be an **Array**

This method will behave like `where(key, 'in', val)` method call.

### `whereNotIn(key, val)`

[](#wherenotinkey-val)

- `key` -- the property name of the data
- `val` -- it should be an **Array**

This method will behave like `where(key, 'notin', val)` method call.

### `whereNull(key)`

[](#wherenullkey)

- `key` -- the property name of the data

This method will behave like `where(key, 'null')` or `where(key, '=', null)` method call.

### `whereNotNull(key)`

[](#wherenotnullkey)

- `key` -- the property name of the data

This method will behave like `where(key, 'notnull')` or `where(key, '!=', null)` method call.

### `whereStartsWith(key, val)`

[](#wherestartswithkey-val)

- `key` -- the property name of the data
- `val` -- it should be a String

This method will behave like `where(key, 'startswith', val)` method call.

### `whereEndsWith(key, val)`

[](#whereendswithkey-val)

- `key` -- the property name of the data
- `val` -- it should be a String

This method will behave like `where(key, 'endswith', val)` method call.

### `whereContains(key, val)`

[](#wherecontainskey-val)

- `key` -- the property name of the data
- `val` -- it should be a String

This method will behave like `where(key, 'contains', val)` method call.

### `sum(column)`

[](#sumcolumn)

- `column` -- the property name of the data

**example:**

Let's say you want to find the sum of the *'price'* of the *'products'*. You can do it like this:

```
$q = new Linq($data);
$res = $q->from('products')
    ->where('cat', '=', 1)
    ->sum('price');
```

If the data you are aggregating is plain array, you don't need to pass the 'column' parameter.

### `count()`

[](#count)

It will return the number of elements in the collection.

**example:**

Let's say you want to find how many elements are in the *'products'* property. You can do it like:

```
$q = new Linq($data);
$res = $q->from('products')
    ->where('cat', '=', 1)
    ->count();
```

See detail example [here](examples/count.php).

### `size()`

[](#size)

This is an alias method of `count()`.

### `max(column)`

[](#maxcolumn)

- `column` -- the property name of the data

**example:**

Let's say you want to find the maximum of the *'price'* of the *'products'*. You can do it like this:

```
$q = new Linq($data);
$res = $q->from('products')
    ->where('cat', '=', 1)
    ->max('price);
```

If the data you are querying is plain array, you don't need to pass the 'column' parameter.

### `min(column)`

[](#mincolumn)

- `column` -- the property name of the data

**example:**

Let's say you want to find the minimum of the *'price'* of the *'products'*. You can do it like this:

```
$q = new Linq($data);
$res = $q->from('products')
    ->where('cat', '=', 1)
    ->min('price');
```

If the data you are querying is plain array, you don't need to pass the 'property' parameter.

### `avg(column)`

[](#avgcolumn)

- `column` -- the property name of the data

**example:**

Let's say you want to find the average of the *'price'* of the *'products'*. You can do it like this:

```
$q = new Linq($data);
$res = $q->from('products')
    ->where('cat', '=', 1)
    ->avg('price');
```

If the data you are querying is plain array, you don't need to pass the 'column' parameter.

### `first()`

[](#first)

It will return the first element of the collection.

**example:**

```
$q = new linq('data.json');
$res = $q->from('products')
    ->where('cat', '=', 1)
    ->first();
```

### `last()`

[](#last)

It will return the last element of the collection.

**example:**

```
$q = new Linq($data);
$res = $q->from('products')
    ->where('cat', '=', 1)
    ->last();
```

### `nth(index)`

[](#nthindex)

- `index` -- index of the element to be returned.

It will return the nth element of the collection. If the given index is a **positive** value, it will return the nth element from the beginning. If the given index is a **negative** value, it will return the nth element from the end.

**example:**

```
$q = new Linq($data);
$res = $q->from('products')
  ->where('cat', '=', 1)
  ->nth(2);
```

### `exists()`

[](#exists)

It will return **true** if the element is not **empty** or not **null** or not an **empty array** or not an **empty object**.

**example:**

Let's say you want to find how many elements are in the *'products'* property. You can do it like:

```
$q = new Linq($data);
$res = $q->from('products')
    ->where('cat', '=', 1)
    ->exists();
```

### `groupBy(column)`

[](#groupbycolumn)

- `column` -- The property by which you want to group the collection.

**example:**

Let's say you want to group the *'users'* data based on the *'location'* property. You can do it like:

```
$q = new Linq($data);
$res = $q->from('users')
    ->groupBy('location')
    ->get();
```

See detail example [here](examples/group-by.php).

### `sort(order)`

[](#sortorder)

- `order` -- If you skip the *'order'* property the data will be by default ordered as **ascending**. You need to pass **'desc'** as the *'order'* parameter to sort the data in **descending** order. Also, you can pass a compare function in *'order'* parameter to define your own logic to order the data.

**Note:** This method should be used for plain Array. If you want to sort an Array of Objects you should use the **sortBy()** method described later.

**example:**

Let's say you want to sort the *'arr'* data. You can do it like:

```
$q = new Linq();
$res = $q->collect([7, 5, 9, 1, 3])
    ->sort();
```

See detail example [here](examples/sort.php).

### `sortBy(column, order)`

[](#sortbycolumn-order)

- `column` -- You need to pass the column name on which the sorting will be done.
- `order` -- If you skip the *'order'* property the data will be by default ordered as **ascending**. You need to pass **'desc'** as the *'order'* parameter to sort the data in **descending** order. Also, you can pass a compare function in *'order'* parameter to define your own logic to order the data.

**Note:** This method should be used for Array of Objects. If you want to sort a plain Array you should use the **sort()** method described earlier.

**example:**

Let's say you want to sort the *'price'* data of *'products'*. You can do it like:

```
$q = new Linq($data);
$res = $q->from('products')
    ->where('cat', '=', 1)
    ->sortBy('price', 'desc');
```

### `reset(data)`

[](#resetdata)

- `data` -- can be a JSON string or a PHP array. If no data passed in the `data` parameter, the `Linq` Object instance will be reset to previously initialized data.

At any point, you might want to reset the Object instance to a completely different set of data and then query over it. You can use this method in that case.

### `copy()`

[](#copy)

It will return a complete clone of the Object instance.

Example Files
-------------

[](#example-files)

> There will be an example file for any of above methods soon.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~10 days

Total

5

Last Release

1729d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8cbcaf2d9ebcdc5282f9dde8aeb9e6cff2d8bd8735b21669fb242e3a47dd7642?d=identicon)[penobit](/maintainers/penobit)

---

Top Contributors

[![penobit](https://avatars.githubusercontent.com/u/60283818?v=4)](https://github.com/penobit "penobit (7 commits)")

---

Tags

arrayjsonlinqphpphparrayquerylinq

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[athari/yalinqo

YaLinqo, a LINQ-to-objects library for PHP

4561.2M5](/packages/athari-yalinqo)[nahid/qarray

QArray is a PHP abstraction for querying array

108403.2k2](/packages/nahid-qarray)[ginq/ginq

LINQ to Object inspired DSL for PHP

192257.5k3](/packages/ginq-ginq)[dotty/dotty

Easy access to array data using dot notation

1293.8k1](/packages/dotty-dotty)

PHPackages © 2026

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