PHPackages                             ajant/simple-array-library - 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. ajant/simple-array-library

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

ajant/simple-array-library
==========================

Simple library containing convenient array handling methods

4.2.1(10y ago)317.4k2[1 PRs](https://github.com/ajant/SimpleArrayLibrary/pulls)1MITPHPPHP &gt;=5.4.0

Since Mar 7Pushed 8y ago1 watchersCompare

[ Source](https://github.com/ajant/SimpleArrayLibrary)[ Packagist](https://packagist.org/packages/ajant/simple-array-library)[ Docs](https://github.com/ajant/SimpleArrayLibrary)[ RSS](/packages/ajant-simple-array-library/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (2)Versions (17)Used By (1)

SimpleArrayLibrary
==================

[](#simplearraylibrary)

[![Build Status](https://camo.githubusercontent.com/194dea33fa4637e27769deb41ebb5a2b3908248ea8833a343c696ee53b5ec9cc/68747470733a2f2f7472617669732d63692e6f72672f616a616e742f53696d706c6541727261794c6962726172792e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/ajant/SimpleArrayLibrary)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/e90b6776a2ee9950bd00ede8bfb179e650f9e6c378a52c96be1b81cf6a7c70ec/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f616a616e742f53696d706c6541727261794c6962726172792f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/ajant/SimpleArrayLibrary/?branch=master)[![Coverage Status](https://camo.githubusercontent.com/b9e854a88919e044e4177489991b4cdfe3553d425774598aff13ddd5a282b238/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f616a616e742f53696d706c6541727261794c6962726172792f62616467652e7376673f6272616e63683d6d617374657226736572766963653d676974687562)](https://coveralls.io/github/ajant/SimpleArrayLibrary?branch=master)

Library containing convenient array handling methods. These are methods that I needed to create for myself during work on various projects, If you have any methods you'd like to see added, let me know:

Requirements
============

[](#requirements)

You'll need: PHP version 5.4+

Quickstart
==========

[](#quickstart)

Install the latest version with composer:

```
require "ajant/simple-array-library": ~4.1.0

```

Auto-load the library:

```
use SimpleArrayLibrary/SimpleArrayLibrary

```

and you're ready to go.

Usage examples
==============

[](#usage-examples)

For additional help, look at the tests, additional input scenarios are tested. addConfigRow
-------------------------------------------------------------------------------------------

[](#for-additional-help-look-at-the-tests-additional-input-scenarios-are-testedaddconfigrow)

Method is intended to remove need for configuration files that need to be parsed into php arrays, since that carries significant performance penalties. It combines readability of ini file with speed of having configurations defined directly on php level. First parameter represents configuration array, second parameter represents a numerical array (in case of associative array, exception is thrown) of nested keys of the new configuration row. Third parameter represents the value that is being mapped. property, that is being added. Method returns configuration with the new row added, or throws exception if the same combination of keys has already been used.

```
SimpleArrayLibrary::addConfigRow(array('foo' => array('baz' => 1)), array('foo', 'bar'), 2); // array('foo' => array('baz' => 1, 'bar' => 2))
$config = array();
$config = SimpleArrayLibrary::addConfigRow($config, array('foo', 'bar'), new stdClass());
$config = SimpleArrayLibrary::addConfigRow($config, array('foo', 'baz'), 1);
$config = SimpleArrayLibrary::addConfigRow($config, array('foo', 3), array(1, 2));
// $config = array('foo' => array('bar' => stdClass(), 'baz' => 1, 3 => array(1, 2)))
$config = SimpleArrayLibrary::addConfigRow($config, array('foo', 3), false); // exception because of repeated  array('foo', 3) path

```

allElementsEqual
----------------

[](#allelementsequal)

Checks whether all elements of the array are equal and optionally if they are all equal to specified value.

```
SimpleArrayLibrary::allElementsEqual(array(1, 1)); // true
SimpleArrayLibrary::allElementsEqual(array(1, 2)); // false
SimpleArrayLibrary::allElementsEqual(array(1, 1), 1); // true
SimpleArrayLibrary::allElementsEqual(array(1, 1), 2); // false

```

castColumns
-----------

[](#castcolumns)

Attempts to cast specified columns of all rows of the two dimensional array to specified type. Allowed types are represented by constants: TYPE\_INT, TYPE\_STRING, TYPE\_FLOAT, TYPE\_BOOL, TYPE\_ARRAY, TYPE\_OBJECT. Any other type will cause exception to be thrown. User should know how type casting works in PHP as this method provides no protection from possible casting errors.

Third parameter must be a boolean, otherwise exception is thrown. If third parameter is set to true, columns defined as keys in the second parameter have to be present, otherwise exception is thrown.

```
SimpleArrayLibrary::castColumns(array(array('a' => '2')), array('a' => SimpleArrayLibrary::TYPE_INT)); // array(array('a' => 2))
SimpleArrayLibrary::castColumns(array(array()), array('a' => SimpleArrayLibrary::TYPE_INT), false); // array(array())
SimpleArrayLibrary::castColumns(array(array('a' => '1'), array('b' => 'foo')), array('a' => SimpleArrayLibrary::TYPE_INT), false); // array(array('a' => 1), array('b' => 'foo'))

```

countMaxDepth
-------------

[](#countmaxdepth)

Count maximum depth of the array (number of nested sub-arrays) recursively. Library contains [non-recursive method](https://github.com/ajant/SimpleArrayLibrary/blob/master/README.md#countmaxdepthiterative) doing th same thing. Recursive method is faster, but in case of very deep arrays may cause memory usage problems.

```
SimpleArrayLibrary::countMaxDepth(array()); // 1
SimpleArrayLibrary::countMaxDepth(array(1, array())); // 2
SimpleArrayLibrary::countMaxDepth(array(array('foo'))); // 2

```

countMaxDepthIterative
----------------------

[](#countmaxdepthiterative)

Count maximum depth of the array (number of nested sub-arrays). Library contains [recursive method](https://github.com/ajant/SimpleArrayLibrary/blob/master/README.md#countmaxdepth) doing th same thing. Recursive method is faster, but in case of very deep arrays may cause memory usage problems.

```
SimpleArrayLibrary::countMaxDepthIterative(array()); // 1
SimpleArrayLibrary::countMaxDepthIterative(array(1, array())); // 2
SimpleArrayLibrary::countMaxDepthIterative(array(array('foo'))); // 2

```

countMinDepth
-------------

[](#countmindepth)

Count minimum depth of the array (number of nested sub-arrays) recursively.

Second parameter must be an integer, or exception is thrown. This is a recursive method, second parameter is used for recursive calls and should not be used from the outside of the method.

```
SimpleArrayLibrary::countMinDepth(array()); // 1
SimpleArrayLibrary::countMinDepth(
    array(
        1,
        array()
    )
); // 1
SimpleArrayLibrary::countMinDepth(1); // 0
SimpleArrayLibrary::countMinDepth(array(), 1); // 2

```

deleteColumns
-------------

[](#deletecolumns)

Deletes values to columns of the multi-dimensional array (is meant for two-dimensional arrays in particular, will work for three or more dimension, but will only change elements on the second level, is not recursive)

```
SimpleArrayLibrary::deleteColumns(array(array('foo' => 2), array()), array('foo')); // array(array(), array())
SimpleArrayLibrary::deleteColumns(array(array(), array()), array('foo')); // array(array(), array())
SimpleArrayLibrary::deleteColumns(array(array('foo' => 2, 'bar' => 1), array()), array('foo', 'bar')); // array(array(), array())

```

getColumns
----------

[](#getcolumns)

Retrieves values of required columns out of the multi-dimensional array.

First parameter must be array of arrays (matrix) otherwise it makes no sense to search for columns &amp; exception is thrown.

Second parameter must be an array of elements that could be used as array indexes, otherwise exception is thrown.

Third parameter must be a boolean, otherwise exception is thrown.

```
SimpleArrayLibrary::getColumns(
    array(
        array('a' => 1),
        array(1)
    ),
    array('a')
); // array('a' => array(1))
SimpleArrayLibrary::getColumns(
    array(
        array(1, 2, 3, 4),
        array(1, 2, 3, 4),
        array(1, 2, 3, 4)
    ),
    array(0 ,2)
) // array(0 => array(1, 1, 1), 2 => array(3, 3, 3))
SimpleArrayLibrary::getColumns(
    array(
        array('a' => 1),
        array(1)
    ),
    array('a'),
    true
); // UnexpectedValueException is thrown, not all rows have all required columns as was requested by the third parameter

```

getRectangularDimensions
------------------------

[](#getrectangulardimensions)

This method checks if array is "rectangular" or in other words, whether all sub-arrays have equal number of elements, recursively, and how many elements there are at each level of recursion, if it is rectangular.

```
SimpleArrayLibrary::getRectangularDimensions(array(1)); // array(1)
SimpleArrayLibrary::getRectangularDimensions(array(1, array(1))); // -1, not "rectangular"
SimpleArrayLibrary::getRectangularDimensions(
    array(                    // 3 elements
        array(                // 1 element
            array(1, 2, 3, 4) // 4 elements
        ),
        array(                // 1 element
            array(1, 2, 3, 4) // 4 elements
        ),
        array(                // 1 element
            array(1, 2, 3, 4) // 4 elements
        )
    )
); // array(4, 1, 3)

```

hasAllKeys
----------

[](#hasallkeys)

Checks if all required keys are present inside an array, regardless of values.

```
SimpleArrayLibrary::hasAllKeys(array('a' => 1), array('a')); // true
SimpleArrayLibrary::hasAllKeys(array(), array()); // true
SimpleArrayLibrary::hasAllKeys(array('b' => 1), array('a', 'b')); // false

```

hasAllValues
------------

[](#hasallvalues)

Checks if all required values are present inside an array, regardless of keys. Values must not be arrays.

```
SimpleArrayLibrary::hasAllValues(array('a' => 1), array(1)); // true
SimpleArrayLibrary::hasAllValues(array(), array()); // true
SimpleArrayLibrary::hasAllValues(array('b', 1), array('a', 'b')); // false

```

hasAllValuesMultiDimensional
----------------------------

[](#hasallvaluesmultidimensional)

Checks if all required values are present inside an array, regardless of keys. Values may be arrays.

```
SimpleArrayLibrary::hasAllValuesMultiDimensional(array('a' => array(1)), array(array(1))); // true
SimpleArrayLibrary::hasAllValuesMultiDimensional(array(array()), array(array())); // true
SimpleArrayLibrary::hasAllValuesMultiDimensional(array('b', 1), array('a', 'b')); // false

```

hasOnlyKeys
-----------

[](#hasonlykeys)

Checks if all required keys are present inside an array, and no other keys.

```
SimpleArrayLibrary::hasOnlyKeys(array('a' => 1, 2), array('a', 0)); // true
SimpleArrayLibrary::hasOnlyKeys(array('b' => 1, 1), array('b', 0, 1)); // false
SimpleArrayLibrary::hasOnlyKeys(array('b' => 1, 1), array('b')); // false

```

haveSameKeys
------------

[](#havesamekeys)

Checks if two arrays have equal keys, regardless of values.

```
SimpleArrayLibrary::haveSameKeys(array('a' => 1), array('a' => 2)); // true
SimpleArrayLibrary::haveSameKeys(array(), array()); // true
SimpleArrayLibrary::haveSameKeys(array(1, 'a' => 1), array(2)); // false

```

haveSameValues
--------------

[](#havesamevalues)

Checks if two arrays have equal values, regardless of keys.

```
SimpleArrayLibrary::haveSameValues(array('a' => 1), array(1)); // true
SimpleArrayLibrary::haveSameValues(array(), array()); // true
SimpleArrayLibrary::haveSameValues(array(1), array(2)); // false

```

insertSubArray
--------------

[](#insertsubarray)

Inserts the sub-array into the array in the place to which sub-array's keys point to

Third and forth parameters must be booleans, otherwise exception is thrown. If third parameter is set to true, insertion will overwrite existing value inside the array, pointed to the sub-array's keys, if any. If third parameter is set to true, forth parameter is not used. If forth parameter is set to true nad third parameter is set to false, existing value will be left unchanged, but if both third and forth parameters were set to false and sub-array keys point to already existing value, exception will be thrown

```
SimpleArrayLibrary::insertSubArray(array('foo' => 1), array('bar' => 2)); // array('foo' => 1, 'bar' => 2)
SimpleArrayLibrary::insertSubArray(array('foo' => 1), array('foo' => 2), true, false); // array('foo' => 2)
SimpleArrayLibrary::insertSubArray(array('foo' => 1), array('foo' => 2), false, true); // array('foo' => 1)
SimpleArrayLibrary::insertSubArray(array('foo' => 1), array('foo' => 2), false, false); // exception

```

isAssociative
-------------

[](#isassociative)

Checks whether array has any associative keys.

```
SimpleArrayLibrary::isAssociative(array('a' => 1, array(1))); // true
SimpleArrayLibrary::isAssociative(array(1, 1)); // false
SimpleArrayLibrary::isAssociative(array()); // false

```

isNumeric
---------

[](#isnumeric)

Checks whether array has all numeric keys starting with zero and progressing by one for each new element.

```
SimpleArrayLibrary::isNumeric(array('a' => 1, array(1))); // false
SimpleArrayLibrary::isNumeric(array(1, 1)); // true
SimpleArrayLibrary::isNumeric(array()); // false

```

isStructureSame
---------------

[](#isstructuresame)

Checks whether 2 arrays have same structure (depth and keys). Values of leaf nodes are ignored, only keys are compared.

```
SimpleArrayLibrary::isStructureSame(1, 'a'); // true
SimpleArrayLibrary::isStructureSame(array(1, array(1)), array(2, array(3)); // true
SimpleArrayLibrary::isStructureSame(array(1, array(1)), array(1, 1)); // false
SimpleArrayLibrary::isStructureSame(array(), array()); // true

```

isSubArray
----------

[](#issubarray)

Checks whether array is sub-array of the other array (whether all key-value pairs of sub-array are present in array).

Third parameter must be a boolean, otherwise exception is thrown. If third parameter is set to true, strict comparison is used (===) when comparing array values, otherwise regular comparison is used (==).

```
SimpleArrayLibrary::isSubArray(array(2, 1), array(2)); // true
SimpleArrayLibrary::isSubArray(array('a' => 1, 'b' => array(1)), array('c' => 1)); // false
SimpleArrayLibrary::isSubArray(array('a' => 1, 'b' => array(1)), array('a' => 2)); // false

```

selectRandomArrayElements
-------------------------

[](#selectrandomarrayelements)

Select sub-array of random elements. Keys association is preserved. If required number of random elements is equal or bigger then number of original array members, entire array is returned.

Second parameter must be positive integer or string representation of the positive integer, otherwise exception is thrown.

```
SimpleArrayLibrary::selectRandomArrayElements(array('foo' => 2), 1); // array('foo' => 2)
SimpleArrayLibrary::selectRandomArrayElements(array(1, 2), 1); // array(1) OR array(1 => 2)
SimpleArrayLibrary::selectRandomArrayElements(array('foo' => 2, 'bar'), 3); // array('foo' => 2, 'bar')

```

setColumn
---------

[](#setcolumn)

Sets values to columns of the multi-dimensional array (is meant for two-dimensional arrays in particular, will work for three or more dimension, but will only change elements on the second level) to the specified value

Forth and fifth parameters must be booleans, otherwise exception is thrown. If forth parameter is set to true, column will be added to the rows in which it's missing. If fifth parameter is set to true, value of the column will be overwritten in rows in which it already exists

```
SimpleArrayLibrary::setColumn(array(array('foo' => 2), array()), 'foo', 1); // array(array('foo' => 1), array('foo' => 1))
SimpleArrayLibrary::setColumn(array(array('foo' => 2), array()), 'foo', 1, false, false); // array(array('foo' => 2), array())
SimpleArrayLibrary::setColumn(array(array('foo' => 2), array()), 'foo', 1, true, false); // array(array('foo' => 2), array('foo' => 1))

```

transpose
---------

[](#transpose)

Transpose a matrix

Turns rows into columns, and columns into rows. This is very handy when you need to reshape a dataset for a report or plot.

```
SimpleArrayLibrary::transpose(array(array(1, 2, 3). array(4, 5, 6))); // array(array(1, 4), array(2, 5), array(3, 6))

```

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 81.8% 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 ~28 days

Total

14

Last Release

3714d ago

Major Versions

1.1.0 → 2.0.02015-09-14

2.2.0 → 3.0.02015-12-01

3.0.0 → 4.0.02015-12-25

### Community

Maintainers

![](https://www.gravatar.com/avatar/7ace9df26b5d3e54aaeadc86838530603512f34ba8cc5ff8565bcc9e9ccc09d5?d=identicon)[ajant](/maintainers/ajant)

---

Top Contributors

[![ajant](https://avatars.githubusercontent.com/u/5788992?v=4)](https://github.com/ajant "ajant (27 commits)")[![zivkovicp](https://avatars.githubusercontent.com/u/213259?v=4)](https://github.com/zivkovicp "zivkovicp (5 commits)")[![johnhout](https://avatars.githubusercontent.com/u/1277118?v=4)](https://github.com/johnhout "johnhout (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ajant-simple-array-library/health.svg)

```
[![Health](https://phpackages.com/badges/ajant-simple-array-library/health.svg)](https://phpackages.com/packages/ajant-simple-array-library)
```

###  Alternatives

[hansschouten/laravel-pagebuilder

A drag and drop pagebuilder to manage pages in any Laravel project.

88027.8k](/packages/hansschouten-laravel-pagebuilder)[naoray/eloquent-model-analyzer

Helpful methods to gain more information about eloquent model classes.

16431.1k1](/packages/naoray-eloquent-model-analyzer)

PHPackages © 2026

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