PHPackages                             apollonin/numphp - 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. apollonin/numphp

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

apollonin/numphp
================

Library for numbers manipulation.

1.9.0(7y ago)832138[1 issues](https://github.com/apollonin/numphp/issues)MITPHPPHP &gt;=5.5.0

Since Jan 16Pushed 7y ago3 watchersCompare

[ Source](https://github.com/apollonin/numphp)[ Packagist](https://packagist.org/packages/apollonin/numphp)[ RSS](/packages/apollonin-numphp/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (1)Dependencies (1)Versions (13)Used By (0)

Numphp
======

[](#numphp)

[![Build Status](https://camo.githubusercontent.com/afad592b648ea33977922c986cb0f2300083bfb0d79768eec5273751510a7bbb/68747470733a2f2f7472617669732d63692e6f72672f61706f6c6c6f6e696e2f6e756d7068702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/apollonin/numphp)[![Latest Stable Version](https://camo.githubusercontent.com/da54d691ede54a3318686d6ea5357f406692db52f8fe2e7084adc4f937bd2e59/68747470733a2f2f706f7365722e707567782e6f72672f61706f6c6c6f6e696e2f6e756d7068702f762f737461626c65)](https://packagist.org/packages/apollonin/numphp)[![Total Downloads](https://camo.githubusercontent.com/c0a5b2049bdffe147ef5d37ff0bebee67859cc398277dc39ef98a06b251b8965/68747470733a2f2f706f7365722e707567782e6f72672f61706f6c6c6f6e696e2f6e756d7068702f646f776e6c6f616473)](https://packagist.org/packages/apollonin/numphp)[![License](https://camo.githubusercontent.com/d63a57a4d5f4f0f937bfa0d455dd1236ae18bc44f5d745221c9b9d59c8d284e3/68747470733a2f2f706f7365722e707567782e6f72672f61706f6c6c6f6e696e2f6e756d7068702f6c6963656e7365)](https://packagist.org/packages/apollonin/numphp)[![codecov](https://camo.githubusercontent.com/f7d01bf0e50f70120887e7737eaa219438fd8ab4fae7fb76f4a865667f3f9146/68747470733a2f2f636f6465636f762e696f2f67682f61706f6c6c6f6e696e2f6e756d7068702f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/apollonin/numphp)[![Maintainability](https://camo.githubusercontent.com/450986614a66c88f5819804c19f477a016bac4bfe41a13c271f3252723fd42f3/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f39636461366430653765373936373930306666322f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/apollonin/numphp/maintainability)[![Test Coverage](https://camo.githubusercontent.com/fb8dbbea650d641202ef82888dcd135151379e56436bf30807a6d945b669cde1/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f39636461366430653765373936373930306666322f746573745f636f766572616765)](https://codeclimate.com/github/apollonin/numphp/test_coverage)

Numphp is a library for number manipulations. If you have an array of numbers, numphp gives you an ability to perform a wide range of useful operations.

Contributions are highly appreciated.

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

[](#installation)

### With composer

[](#with-composer)

```
composer require apollonin/numphp

```

Available features
------------------

[](#available-features)

**Arrays**

- get item by index
- get items by array of indexes
- get items by condition
    - eq, gt, gte, lt, lte, neq - equals, greater than, and so on
- get items by complex conditions
    - b\_and, b\_or - bitwise AND and OR
- set items values according to conditions, indexes or slices
- apply math operations to whole array
    - mul, div, add, sub, pow, mod
- get slice of array
- get statistical values from array
    - count, max, mean, median, min, sum
    - describe - special method that displays all above values
- Get dimensional data
    - shape
    - dimension
- Concatenate arrays

np\_array also has classical array behaviour. So you are able to iterate through it as usual.

**Matrix**

Matrix is a special case of arrays. At the moment, we only support 2d matrices. Full support for n-dimensional matrices is on the way.

You can perform all the same operations and comparisons as with arrays. Refer to Matrix section below in usage examples.

**Dimensional Manipulation**

You are able to change dimensions for existed array or matrix. Use `flatten` or `reshape` methods.

**Random Module**

Numphp also provides convenient ways to generate new np\_arrays and populate them with random values. Available methods are

- rand
- randint

If `size` parameter is given, returns np\_array with appropriate elements. Otherwise, it returns single random value.

**Generators**

For quick stub array creation you may use these convenient predefined methods

- ones - creates array full of 1
- zeros - creates array full of 0
- full- creates array full of provided fill\_value
- arange - creates evenly spaced values within a given interval.
- fib - creates Fibonacci numbers
- formula - returns sequence of numbers, based on provided formula

Usage Examples
--------------

[](#usage-examples)

### Indexing

[](#indexing)

**create new array**

```
$list = new np_array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
```

**Get items by their indexes**

```
$result = $list[[2,3]];

// result
[2, 3]
```

To get item as single value - pass index as single value as well

```
$result = $list[1];

// result
1
```

**Get items by condition**

```
$result = $list[$list->gt(5)];

// result
[6, 7, 8, 9]
```

You may also access index by string representations of comparison.

```
// gives the same result as above
$result = $list[$list['> 5']];
```

*Important note about conditional indexing: conditional operator returns masking array:*

```
  $mask = $list->gt(5);

  // mask
  [false, false, false, false, false, false, true, true, true, true]

  // and then
  $result = $list[$mask];

  // result
  [6, 7, 8, 9]
```

You also can pass another array as an argument. In this case the comparison will be applied for each element respectively.

```
$result = $list[$list->gt([5, 6, 7, 8, 9, 3, 4, 5, 6, 7])];

// result
[6, 7, 8, 9]
```

**Get items by conditions**

*b\_and* - "bitwise" and

```
$resuilt = $list[Bitwise::b_and($list->gte(5), $list->lt(8))];

// result
[5, 6, 7]
```

**Array-like behaviour**

You may also iterate your np\_array object as usual

```
foreach ($list as $item) {
    echo $item . " ";
}

// output
0 1 2 3 4 5 6 7 8 9
```

### Slicing

[](#slicing)

You may get slices of your np\_array in a very convenient way. Just pass string formatted like `start:[stop][:step]` as index and you'll get result.

```
$result = $list['1:5'];

//result
[1, 2, 3, 4]

$result = $list['1:5:2'];

//result
[1, 3]
```

You can even skip `stop` and `step` values, which means: get all items from `start` to the end of array.

```
$result = $list['1:'];

//result
[1, 2, 3, 4, 5, 6, 7, 8, 9]
```

You may even skip `start` value; it will be considered as 0 in this case

```
$result = $list[':'];

//result
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
```

Negative `start` or `stop` means indexes count from the end of array

```
$result = $list['-7:6'];

//result
[3, 4, 5]
```

### Set item values

[](#set-item-values)

**Set items by indexes**

```
$result = clone($list);
$result[[2,3]] = 999;

// result
[0, 1, 999, 999, 4, 5, 6, 7, 8, 9]
```

**Set items by conditions**

```
$result = clone($list);
$result[$result->gte(5)] = 999;

// result
[0, 1, 2, 3, 4, 999, 999, 999, 999, 999]
```

**Set items by slice**

```
$result = clone($list);
$result['1:3'] = 999;

// result
[0, 999, 999, 3, 4, 5, 6, 7, 8, 9]
```

**Adding new items**

Of course, you may add new items as usual

```
$result = clone($list);
$result[] = 999;

// result
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 999]
```

### Math operations

[](#math-operations)

You are able to apply certain math operations to the whole array. It will apply to each element.

```
$result = $list->add(100);

// result
[100, 101, 102, 103, 104, 105, 106, 107, 108, 109]
```

You may also perform math operation under two np\_arrays

```
$result = $list->add(new np_array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))

//result
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
```

Or event np\_array and normal array!

```
$result = $list->add([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);

//result
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
```

### Random module

[](#random-module)

**Create array with random floats**

```
use numphp\Random\Random;

$result = Random::rand(5)

// result
[0.64488127438579, 0.21702189986455, 0.96931800524207, 0.78197341448719, 0.89214772632911]
```

**Array with random integers**

```
use numphp\Random\Random;

$result = Random::randint(5, 15, 10);

// result
[13, 9, 12, 14, 6, 15, 8, 9, 5, 13]
```

### Generators module

[](#generators-module)

**create array full of zeros, ones or fill\_value**

```
use numphp\Generator\Generator;

$result = Generator::zeros(5);

//result
[0, 0, 0, 0, 0]

$result = Generator::ones(5);

//result
[1, 1, 1, 1, 1]

$result = Generator::full(5, 999);

//result
[999, 999, 999, 999, 999]
```

**Create array within a range and given interval**

```
use numphp\Generator\Generator;

$result = Generator::arange(1, 15, 2);

//result
[1, 3, 5, 7, 9, 11, 13]
```

**Generate N [Fibonacci](https://en.wikipedia.org/wiki/Fibonacci_number) numbers**

```
use numphp\Generator\Generator;

$result = Generator::fib(6);

//result
[1, 1, 2, 3, 5, 8]
```

**Generate numbers according to formula**

Provide [callable](http://php.net/manual/en/language.types.callable.php) as a first argument. It must return value, that will be used in sequence.

```
use numphp\Generator\Generator;

$result = Generator::formula(function($n){return 2*$n+1;}, 1, 5);

//result
[3, 5, 7, 9]
```

**Generate matrix with given diagonal**

```
$matrix = Generator::diagonal([5, 3, 1]);

// matrix
[[5, 0, 0],
[0, 3, 0],
[0, 0, 1]]

```

### Matrix operations

[](#matrix-operations)

Generally the syntax and features are the same as for arrays

**Creation**

```
$matrix = new np_array([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]);

// matrix
[[ 0,  1,  2,  3],
 [ 4,  5,  6,  7],
 [ 8,  9, 10, 11]]

```

**Indexing**

Indexing is done in respect to X-axis (rows)

```
$result = $matrix[0];

//result
[0, 1, 2, 3]

```

**Slicing**

```
$result = $matrix['1:3'];

//result
[[ 4,  5,  6,  7],
 [ 8,  9, 10, 11]]

```

**Comparisons**

```
$result = $matrix[$matrix->gt(5)];

//result
[6, 7, 8, 9, 10, 11]

```

Keep in mind 'masking' feature

```
$mask = $matrix->gt(5);

//mask
[[false, false, false, false],
[false, false, true, true],
[true, true, true, true]]

```

**Changing values**

```
$matrix[$matrix->gte(5)] = 999;

//matrix
[[  0,   1,   2,   3],
 [  4, 999, 999, 999],
 [999, 999, 999, 999]]

```

**Math operations**

```
$result = $matrix->mul(5);

//result
[[ 0,  5, 10, 15],
 [20, 25, 30, 35],
 [40, 45, 50, 55]]

```

**Get shape of matrix**

```
$shape = $matrix->shape;

//shape: [rows, cols]
[3, 4]

```

And if you just need count of dimensions

```
$dimensions = $matrix->dimensions;

//dimensions
2

```

**Diagonal**

```
$result = $matrix->diagonal();

//result
[0, 5, 10]

```

or you can set offset for diagonal

```
$result = $matrix->diagonal(2);

//result
[2, 7]

```

Changing dimensions
-------------------

[](#changing-dimensions)

**Flatten matrix**

You can get 1-D array from matrix.

```
$result = $matrix->flatten();

//result
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

```

**Reshaping**

You also can change current shape of matrix to any desired.

```
$result = $matrix->reshape([6, 2]);

//result
[[ 0,  1],
 [ 2,  3],
 [ 4,  5],
 [ 6,  7],
 [ 8,  9],
 [10, 11]]

```

Concatenation
-------------

[](#concatenation)

**concatenate arrays**

You can concatenate two or more arrays into one. Logic is similar to [array\_merge](http://php.net/manual/en/function.array-merge.php) native php method

```
$l1 = Generator::arange(1, 5);
$l2 = Generator::arange(5, 8);
$l3 = Generator::arange(8, 10);

$result = np::concatenate($l1, $l2, $l3)

//result
[1, 2, 3, 4, 5, 6, 7, 8, 9]

```

**concatenate matrixes**

The same logic can be applied to matrixes

```
$m2 = Generator::ones([1, 4]);
$result = np::concatenate($matrix, $m2)

//result
[[ 0,  1,  2,  3],
 [ 4,  5,  6,  7],
 [ 8,  9, 10, 11],
 [ 1,  1,  1,  1]]

```

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 95% 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 ~39 days

Recently: every ~103 days

Total

12

Last Release

2660d ago

PHP version history (2 changes)1.0.0PHP &gt;=5.3.0

1.8.1PHP &gt;=5.5.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2346916?v=4)[Dmitriy](/maintainers/apollonin)[@apollonin](https://github.com/apollonin)

---

Top Contributors

[![apollonin](https://avatars.githubusercontent.com/u/2346916?v=4)](https://github.com/apollonin "apollonin (151 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (3 commits)")[![carusogabriel](https://avatars.githubusercontent.com/u/16328050?v=4)](https://github.com/carusogabriel "carusogabriel (1 commits)")[![upsilon-den](https://avatars.githubusercontent.com/u/13876311?v=4)](https://github.com/upsilon-den "upsilon-den (1 commits)")[![zhurakovskyi](https://avatars.githubusercontent.com/u/24625069?v=4)](https://github.com/zhurakovskyi "zhurakovskyi (1 commits)")[![dgunay](https://avatars.githubusercontent.com/u/16074091?v=4)](https://github.com/dgunay "dgunay (1 commits)")[![artyuum](https://avatars.githubusercontent.com/u/17199757?v=4)](https://github.com/artyuum "artyuum (1 commits)")

---

Tags

numbersnumphpphparrayfilternumbers

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[doctrine/collections

PHP Doctrine Collections library that adds additional functionality on top of PHP arrays.

6.0k423.8M1.4k](/packages/doctrine-collections)[symfony/property-access

Provides functions to read and write from/to an object or array using a simple string notation

2.8k309.5M3.0k](/packages/symfony-property-access)[nette/utils

🛠 Nette Utils: lightweight utilities for string &amp; array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.

2.1k417.9M1.7k](/packages/nette-utils)[league/config

Define configuration arrays with strict schemas and access values with dot notation

565323.7M35](/packages/league-config)[cuyz/valinor

Dependency free PHP library that helps to map any input into a strongly-typed structure.

1.5k11.7M157](/packages/cuyz-valinor)[openlss/lib-array2xml

Array2XML conversion library credit to lalit.org

31053.4M49](/packages/openlss-lib-array2xml)

PHPackages © 2026

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