PHPackages                             philiprehberger/php-array-query - 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. philiprehberger/php-array-query

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

philiprehberger/php-array-query
===============================

SQL-like fluent query builder for filtering, sorting, and transforming arrays

v1.0.2(4d ago)11MITPHPPHP ^8.2CI passing

Since Mar 13Pushed yesterdayCompare

[ Source](https://github.com/philiprehberger/php-array-query)[ Packagist](https://packagist.org/packages/philiprehberger/php-array-query)[ Docs](https://github.com/philiprehberger/php-array-query)[ RSS](/packages/philiprehberger-php-array-query/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (3)Versions (3)Used By (0)

PHP Array Query
===============

[](#php-array-query)

[![Tests](https://github.com/philiprehberger/php-array-query/actions/workflows/tests.yml/badge.svg)](https://github.com/philiprehberger/php-array-query/actions/workflows/tests.yml)[![Latest Version on Packagist](https://camo.githubusercontent.com/6035f8886b1b5ded7bc7a122f036dc96fdb48fc521a91a8ab62d3ed2b85c3fe5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7068696c69707265686265726765722f7068702d61727261792d71756572792e737667)](https://packagist.org/packages/philiprehberger/php-array-query)[![License](https://camo.githubusercontent.com/d2fcea4e40ee34bc875c95cf828df6aedf59c95f2c8e416bd0f911373f6fca72/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7068696c69707265686265726765722f7068702d61727261792d7175657279)](LICENSE)

SQL-like fluent query builder for filtering, sorting, and transforming arrays.

Requirements
------------

[](#requirements)

- PHP 8.2+

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

[](#installation)

```
composer require philiprehberger/php-array-query
```

Usage
-----

[](#usage)

### Basic Filtering

[](#basic-filtering)

```
use PhilipRehberger\ArrayQuery\ArrayQuery;

$users = [
    ['name' => 'Alice', 'age' => 30, 'city' => 'NYC', 'score' => 85],
    ['name' => 'Bob', 'age' => 25, 'city' => 'LA', 'score' => 92],
    ['name' => 'Charlie', 'age' => 35, 'city' => 'NYC', 'score' => 78],
    ['name' => 'Diana', 'age' => 28, 'city' => 'Chicago', 'score' => null],
    ['name' => 'Eve', 'age' => 22, 'city' => 'LA', 'score' => 95],
];

$results = ArrayQuery::from($users)
    ->where('city', '=', 'NYC')
    ->where('age', '>', 25)
    ->get();
// [['name' => 'Alice', ...], ['name' => 'Charlie', ...]]
```

### Sorting

[](#sorting)

```
$results = ArrayQuery::from($users)
    ->sort('age', 'desc')
    ->get();
// Sorted by age descending: Charlie, Alice, Diana, Bob, Eve
```

### Limit &amp; Offset

[](#limit--offset)

```
$results = ArrayQuery::from($users)
    ->sort('age')
    ->offset(1)
    ->limit(3)
    ->get();
// Skip first, take next 3
```

### Select Specific Keys

[](#select-specific-keys)

```
$results = ArrayQuery::from($users)
    ->select(['name', 'age'])
    ->get();
// [['name' => 'Alice', 'age' => 30], ...]
```

### Pluck a Single Column

[](#pluck-a-single-column)

```
$names = ArrayQuery::from($users)
    ->where('city', '=', 'LA')
    ->pluck('name');
// ['Bob', 'Eve']
```

### Group By

[](#group-by)

```
$groups = ArrayQuery::from($users)
    ->groupBy('city');
// ['NYC' => [...], 'LA' => [...], 'Chicago' => [...]]
```

### Aggregates

[](#aggregates)

```
$query = ArrayQuery::from($users)->whereNotNull('score');

$query->sum('score');   // 350
$query->avg('score');   // 87.5
$query->min('score');   // 78
$query->max('score');   // 95
$query->count();        // 4
```

### Additional Filters

[](#additional-filters)

```
// Where In
ArrayQuery::from($users)->whereIn('city', ['NYC', 'LA'])->get();

// Where Null / Not Null
ArrayQuery::from($users)->whereNull('score')->get();
ArrayQuery::from($users)->whereNotNull('score')->get();

// Where Between
ArrayQuery::from($users)->whereBetween('age', 25, 30)->get();

// Like (case-insensitive)
ArrayQuery::from($users)->where('name', 'like', '%ali%')->get();
```

### Dot Notation for Nested Arrays

[](#dot-notation-for-nested-arrays)

```
$items = [
    ['name' => 'Alice', 'address' => ['city' => 'NYC']],
    ['name' => 'Bob', 'address' => ['city' => 'LA']],
];

ArrayQuery::from($items)
    ->where('address.city', '=', 'NYC')
    ->get();
```

API
---

[](#api)

MethodDescription`ArrayQuery::from(array $items)`Create a new query from an array of associative arrays`where(string $key, string $operator, mixed $value)`Filter by comparison (`=`, `==`, `===`, `!=`, ``, `>`, `=`, `
