PHPackages                             tfhinc/ci-ray - 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. tfhinc/ci-ray

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

tfhinc/ci-ray
=============

Ray is an expressive PHP array class for the Codeigniter framework.

v1.1.0(7y ago)39[1 issues](https://github.com/TFHInc/ci-ray/issues)MITPHPPHP &gt;=7.1.0

Since Nov 30Pushed 7y ago1 watchersCompare

[ Source](https://github.com/TFHInc/ci-ray)[ Packagist](https://packagist.org/packages/tfhinc/ci-ray)[ Docs](https://github.com/TFHInc/ci-ray)[ RSS](/packages/tfhinc-ci-ray/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (1)Versions (5)Used By (0)

Ray
===

[](#ray)

[![Latest Version on Packagist](https://camo.githubusercontent.com/5125950c9776408c7c294d78f25973d1b622571ab903874a14657d0ecaf8ad65/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f746668696e632f63692d7261792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tfhinc/ci-ray)[![PHP Version](https://camo.githubusercontent.com/ec884dc3bbb88b4f777a0a4a0228169e00bb9ee945bc16b551456d9ab840312c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f746668696e632f63692d7261792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tfhinc/ci-ray)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/c5c65e1895944b9ada4ca5bdef91c563141b4ab8e57f70acb758bef8528fdba7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f746668696e632f63692d7261792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tfhinc/ci-ray)

Ray is an expressive PHP array library for the [Codeigniter](https://codeigniter.com/) framework.

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

[](#requirements)

- PHP &gt;= 7.1.0
- CodeIgnitor 3.x

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

[](#installation)

```
composer require tfhinc/ci-ray
```

Run the post install command to publish the helper and class files to the appropriate CI directories:

```
composer --working-dir=vendor/tfhinc/ci-ray/ run-script publish-files
```

Loading the Library
-------------------

[](#loading-the-library)

There are a few available options for loading the Warehouse library:

### Using the `ray()` helper function

[](#using-the-ray-helper-function)

The Ray helper function will resolve the `Ray` class via the CI instance. It will either load the class or return the existing class instance:

```
$this->load->helper('ray');
```

### Using the Ray Class

[](#using-the-ray-class)

The Ray class can be instantiated when you require it:

```
$ray = new TFHInc/Ray/Ray();
```

### Using the Ray CI Library

[](#using-the-ray-ci-library)

The Ray class can be loaded like any other CI library:

```
$this->load->library('Ray');
```

Usage
-----

[](#usage)

`Ray` can be used in a variety of ways to manipulate and transfrom arrays.

### Method Usage

[](#method-usage)

`Ray` enables you to interact with arrays using simple methods that return a single value or a transformed array:

```
// Given an array...

$fruit = [
    'lemon' => 'yellow',
    'apple' => 'red',
    'lime' => 'green',
    'pear' => 'green',
];

// ...Get all of the keys except 'apple' and 'lime':

ray($fruit)->except(['apple', 'lime'])->toArray();

/*

    [
        'lemon' => 'yellow',
        'pear' => 'green',
    ]

*/

// ...Get the first value:

ray($fruit)->first();

/*

    'yellow'

*/

// ...Sort by value:

ray($fruit)->sortByValues()->toArray();

/*

    [
        'lime' => 'green',
        'pear' => 'green',
        'apple' => 'red',
        'lemon' => 'yellow',
    ]

*/
```

### Method Chaining

[](#method-chaining)

The power of `Ray` is displayed when chaining methods together to manipulate an array:

```
// Given a multidimensional array...

$fruit_multi = [
    [ 'id' => 1, 'name' => 'lemon', 'color' => 'yellow',    'price' => 2.25, 'qty' => 2 ],
    [ 'id' => 2, 'name' => 'apple', 'color' => 'red',       'price' => 0.99, 'qty' => 12 ],
    [ 'id' => 3, 'name' => 'lime',  'color' => 'green',     'price' => 3.50, 'qty' => 9 ],
    [ 'id' => 4, 'name' => 'pear',  'color' => 'green',     'price' => 2.00, 'qty' => 7 ],
];

// ...Group the array by the 'color' key and only return keys 'red' or 'green':

ray($fruit_multi)->groupBy('color')->only(['red', 'green'])->toArray();

/*
    [
        'red' => [
            [
                'id' => 2,
                'name' => 'apple',
                'color' => 'red',
                'price' => 0.99,
                'qty' => 12,
            ],
        ],
        'green' => [
            [
                'id' => 3,
                'name' => 'lime',
                'color' => 'green',
                'price' => 3.5,
                'qty' => 9,
            ],
            [
                'id' => 4,
                'name' => 'pear',
                'color' => 'green',
                'price' => 2,
                'qty' => 7,
            ],
        ],
    ]
*/

// ...Where the 'color' key is 'green', sum the 'price':

ray($fruit_multi)->where('color', 'green')->sum('price');

/*

    5.5

*/

// ...Where the 'color' key is not 'green', filter the items that have a 'price' greater than 2:

ray($fruit_multi)->whereNot('color', 'green')->filter(function($item, $key) {
    return $item['price'] > 2;
})->toArray();

/*

    [
        [
            'id' => 1,
            'name' => 'lemon',
            'color' => 'yellow',
            'price' => 2.25,
            'qty' => 2,
        ]
    ]

*/

// ...Where the 'color' key is 'green' or 'yellow', count the number of items:

ray($fruit_multi)->whereIn('color', ['green','yellow'])->count();

/*

    3

*/

// ...Retreive a column by the 'color' key and key the transformed array by the `name` key, sort by key:

ray($fruit_multi)->column('color', 'name')->sortByKeys()->toArray();

/*

    [
        'apple' => 'red',
        'lemon' => 'yellow',
        'lime' => 'green',
        'pear' => 'green',
    ]

*/
```

### Method Return Types

[](#method-return-types)

`Ray` methods will return different data types depedent on the desired outcome of the method. Each documented method definition indicates the data type returned.

- The `toArray()` method should be called at the end of the method chaining sequence to return the final transformed `array`:

```
// toArray() returns the final transformed array:

ray($fruit_multi)->column('color', 'name')->sortByKeys()->toArray();

/*

    [
        'apple' => 'red',
        'lemon' => 'yellow',
        'lime' => 'green',
        'pear' => 'green',
    ]

*/
```

- Methods that return a `string` or an `integer` do not require the `toArray()` method. These methods should be called at the end of the method chaining sequence:

```
// count() returns an integer:

ray($fruit_multi)->whereIn('color', ['green','yellow'])->count();

/*

    3

*/

// first() returns a string:

ray($fruit)->first();

/*

    'yellow'

*/
```

Available Methods
-----------------

[](#available-methods)

The following methods are currently available:

- [sortByKeys](#sortbykeys)
- [sortByValues](#sortbyvalues)
- [has](#hasstring-key)
- [contains](#containsmixed-value--mixed-key)
- [sum](#sumstring-key)
- [avg](#avgstring-key)
- [count](#count)
- [values](#values)
- [first](#first)
- [last](#last)
- [except](#exceptarray-keys)
- [only](#onlyarray-keys)
- [unique](#uniquestring-key)
- [groupBy](#groupbystring-key)
- [column](#columnstring-key-string-key_by)
- [where](#wherestring-key-string-value)
- [whereIn](#whereinstring-key-array-values)
- [whereNot](#wherenotstring-key-string-value)
- [whereNotIn](#wherenotinstring-key-array-values)
- [filter](#filtercallable-callback)
- [reduce](#reducecallable-callback)

#### `sortByKeys()`

[](#sortbykeys)

Sort the array by its keys.

```
ray($fruit)->sortByKeys()->toArray();

/*

    Array
    (
        [apple] => red
        [lemon] => yellow
        [lime] => green
        [pear] => green
    )

*/
```

#### `sortByValues()`

[](#sortbyvalues)

Sort the array by its values.

```
ray($fruit)->sortByValues()->toArray();

/*

    Array
    (
        [lime] => green
        [apple] => red
        [lemon] => yellow
    )

*/
```

#### `has(string $key)`

[](#hasstring-key)

Determine if the array contains a given key.

```
ray($fruit_multi)->has('price');

// true

ray($fruit_multi)->has('brand');

// false
```

#### `contains(string $value [, string $key])`

[](#containsstring-value--string-key)

Determine if the array contains a given value.

```
ray($fruit_multi)->contains('green');

// true

ray($fruit_multi)->contains('brown');

// false
```

Optionally provide a key to limit the `contains()` check

```
ray($fruit_multi)->contains('color', 'green');

// true

ray($fruit_multi)->contains('color', 'brown');

// false
```

#### `sum(string $key)`

[](#sumstring-key)

Get the sum of the values for the provided key.

```
ray($fruit_multi)->sum('qty');

// 30

ray($fruit_multi)->sum('price');

// 8.74
```

#### `avg(string $key)`

[](#avgstring-key)

Get the average of the values for the provided key.

```
ray($fruit_multi)->avg('price');

// 2.185
```

#### `count()`

[](#count)

Get the count of the values.

```
ray($fruit_multi)->count();

// 4
```

#### `values()`

[](#values)

Get the values of the array. Can be used to reindex the array with consecutive integers.

```
ray($fruit)->values()->toArray();

/*

    Array
    (
        [0] => yellow
        [1] => red
        [2] => green
        [3] => green
    )

*/
```

#### `first()`

[](#first)

Get the first value of the array.

```
ray($fruit)->first();

// yellow

ray($fruit_multi)->first();

/*

    Array
    (
        [id] => 1
        [name] => lemon
        [color] => yellow
        [price] => 2.25
        [qty] => 2
    )

*/
```

#### `last()`

[](#last)

Get the last value of the array.

```
ray($fruit)->last();

// green

ray($fruit_multi)->last();

/*

    Array
    (
        [id] => 4
        [name] => pear
        [color] => green
        [price] => 2
        [qty] => 7
    )

*/
```

#### `except(array $keys)`

[](#exceptarray-keys)

Get all array elements except for the provided keys.

```
ray($fruit)->except(['apple', 'lime'])->toArray();

/*

    Array
    (
        [lemon] => yellow
        [pear] => green
    )

*/
```

#### `only(array $keys)`

[](#onlyarray-keys)

Only get the array elements for the provided keys.

```
ray($fruit)->only(['apple', 'lime'])->toArray();

/*

    Array
    (
        [apple] => red
        [lime] => green
    )

*/
```

#### `unique([string $key])`

[](#uniquestring-key)

Limit the array by unique value. Optionally limit by unique values of the provided key. The array keys are preserved. If there are duplicate values, the first key/value pair will be retained.

```
ray($fruit)->unique()->toArray();

/*

    Array
    (
        [lemon] => yellow
        [apple] => red
        [lime] => green
    )

*/

ray($fruit_multi)->unique('color')->toArray();

/*

    Array
    (
        [0] => Array
            (
                [id] => 1
                [name] => lemon
                [color] => yellow
                [price] => 2.25
                [qty] => 2
            )

        [1] => Array
            (
                [id] => 2
                [name] => apple
                [color] => red
                [price] => 0.99
                [qty] => 12
            )

        [2] => Array
            (
                [id] => 3
                [name] => lime
                [color] => green
                [price] => 3.5
                [qty] => 9
            )

    )

*/
```

#### `groupBy(string $key)`

[](#groupbystring-key)

Group the array by a given key.

```
ray($fruit_multi)->groupBy('color')->toArray();

/*

    Array
    (
        [yellow] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        [name] => lemon
                        [color] => yellow
                        [price] => 2.25
                        [qty] => 2
                    )

            )

        [red] => Array
            (
                [0] => Array
                    (
                        [id] => 2
                        [name] => apple
                        [color] => red
                        [price] => 0.99
                        [qty] => 12
                    )

            )

        [green] => Array
            (
                [0] => Array
                    (
                        [id] => 3
                        [name] => lime
                        [color] => green
                        [price] => 3.5
                        [qty] => 9
                    )

                [1] => Array
                    (
                        [id] => 4
                        [name] => pear
                        [color] => green
                        [price] => 2
                        [qty] => 7
                    )

            )

    )

*/
```

#### `column(string $key, [string $key_by])`

[](#columnstring-key-string-key_by)

Retreive an entire column from the array. Optionally key the new transformed array by the provided key\_by argument.

```
ray($fruit_multi)->column('color')->toArray();

/*

    Array
    (
        [0] => yellow
        [1] => red
        [2] => green
        [3] => green
    )

*/

ray($fruit_multi)->column('color', 'name')->toArray();

/*

    Array
    (
        [lemon] => yellow
        [apple] => red
        [lime] => green
        [pear] => green
    )

*/
```

#### `where(string $key, string $value)`

[](#wherestring-key-string-value)

Limit the array by a specific key and value.

```
ray($fruit_multi)->where('color', 'green')->toArray();

/*

    Array
    (
        [2] => Array
            (
                [id] => 3
                [name] => lime
                [color] => green
                [price] => 3.5
                [qty] => 9
            )

        [3] => Array
            (
                [id] => 4
                [name] => pear
                [color] => green
                [price] => 2
                [qty] => 7
            )

    )

*/
```

#### `whereIn(string $key, array $values)`

[](#whereinstring-key-array-values)

Limit the array by a specific key and an array of values.

```
ray($fruit_multi)->whereIn('color', ['green', 'yellow'])->toArray();

/*

    Array
    (
        [0] => Array
            (
                [id] => 1
                [name] => lemon
                [color] => yellow
                [price] => 2.25
                [qty] => 2
            )

        [2] => Array
            (
                [id] => 3
                [name] => lime
                [color] => green
                [price] => 3.5
                [qty] => 9
            )

        [3] => Array
            (
                [id] => 4
                [name] => pear
                [color] => green
                [price] => 2
                [qty] => 7
            )

    )

*/
```

#### `whereNot(string $key, string $value)`

[](#wherenotstring-key-string-value)

Limit the array by a given key and value.

```
ray($fruit_multi)->whereNot('color', 'green')->toArray();

/*

    Array
    (
        [0] => Array
            (
                [id] => 1
                [name] => lemon
                [color] => yellow
                [price] => 2.25
                [qty] => 2
            )

        [1] => Array
            (
                [id] => 2
                [name] => apple
                [color] => red
                [price] => 0.99
                [qty] => 12
            )

    )

*/
```

#### `whereNotIn(string $key, array $values)`

[](#wherenotinstring-key-array-values)

Limit the array by a specific key and an array of values.

```
ray($fruit)->whereNotIn('color', ['green', 'yellow'])->toArray();

/*

    Array
    (
        [1] => Array
            (
                [id] => 2
                [name] => apple
                [color] => red
                [price] => 0.99
                [qty] => 12
            )

    )

*/
```

#### `filter(callable $callback)`

[](#filtercallable-callback)

Filter the array by the provided callback.

```
ray($fruit_multi)->filter(function($value, $key) {
    return $value['price'] > 3;
})->toArray();

/*

    Array
    (
        [2] => Array
            (
                [id] => 3
                [name] => lime
                [color] => green
                [price] => 3.5
                [qty] => 9
            )

)

*/

ray($fruit_multi)->filter(function($value, $key) {
    return $value['price'] < 3;
})->toArray();

/*

    Array
    (
        [0] => Array
            (
                [id] => 1
                [name] => lemon
                [color] => yellow
                [price] => 2.25
                [qty] => 2
            )

        [1] => Array
            (
                [id] => 2
                [name] => apple
                [color] => red
                [price] => 0.99
                [qty] => 12
            )

        [3] => Array
            (
                [id] => 4
                [name] => pear
                [color] => green
                [price] => 2
                [qty] => 7
            )

    )

*/
```

#### `reduce(callable $callback)`

[](#reducecallable-callback)

Reduce the array by a callback to a single value.

```
ray($fruit_multi)->reduce(function($carry, $value) {
    return $value['qty'] < 3 ? $carry + $value['qty'] : $carry;
});

// 2
```

Contributing
------------

[](#contributing)

Feel free to create a GitHub issue or send a pull request with any bug fixes. Please see the GutHub issue tracker for isses that require help.

Acknowledgements
----------------

[](#acknowledgements)

- [Colin Rafuse](https://github.com/crafuse)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance13

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

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 ~1 days

Total

4

Last Release

2719d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f149fbf2c1509cded6ca2d6a64a82bb81535d88ec5466d361247d270ccd8e2ca?d=identicon)[crafuse](/maintainers/crafuse)

---

Top Contributors

[![crafuse](https://avatars.githubusercontent.com/u/10606086?v=4)](https://github.com/crafuse "crafuse (6 commits)")

---

Tags

arraycodeignitercodeigniter-libraryrayphprayarraycodeigniterci-ray

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/tfhinc-ci-ray/health.svg)

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

###  Alternatives

[zakirullin/mess

Convenient array-related routine &amp; better type casting

21228.9k2](/packages/zakirullin-mess)

PHPackages © 2026

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