PHPackages                             phpmlkit/ndarray - 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. phpmlkit/ndarray

ActivePlatform-package[Utility &amp; Helpers](/categories/utility)

phpmlkit/ndarray
================

High-performance N-dimensional arrays for PHP, powered by Rust

1.2.1(1mo ago)43197↑250%[1 issues](https://github.com/phpmlkit/ndarray/issues)MITRustPHP ^8.1CI passing

Since Mar 1Pushed 1mo ago1 watchersCompare

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

READMEChangelog (4)Dependencies (10)Versions (5)Used By (0)

 [![NDArray PHP Logo](docs/public/logo.png)](docs/public/logo.png)

NDArray PHP
===========

[](#ndarray-php)

High-performance N-dimensional arrays for PHP, powered by Rust via FFI

 [![Latest Version](https://camo.githubusercontent.com/8de907acbe13c15e8d1f2f6cb7c76539ed7ab449146edf0519bad7c3bc2e9f03/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7068706d6c6b69742f6e6461727261793f7374796c653d666c61742d737175617265)](https://packagist.org/packages/phpmlkit/ndarray) [![GitHub Workflow Status](https://camo.githubusercontent.com/8ff79c8b175de233f3f15e2a6c4d2087ae45936575f52774955dd3880283c86b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7068706d6c6b69742f6e6461727261792f74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/phpmlkit/ndarray/actions) [![Total Downloads](https://camo.githubusercontent.com/3a8480eb6bc46f122c28810d67feec91039ea4bd7b5e14699549aba44ff378a8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7068706d6c6b69742f6e6461727261793f7374796c653d666c61742d737175617265)](https://packagist.org/packages/phpmlkit/ndarray) [![License](https://camo.githubusercontent.com/8bb50fd2278f18fc326bf71f6e88ca8f884f72f179d3e555e20ed30157190d0d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e2e737667)](LICENSE)

Features
--------

[](#features)

- **Blazing Fast** - Zero-copy views, Rust backend, minimal FFI overhead
- **NumPy Compatible** - Familiar API for Python developers
- **Memory Safe** - Automatic memory management via Rust
- **N-Dimensional** - Support for arbitrary dimensions
- **Type Safe** - Multiple data types with automatic inference
- **Scientific Computing** - Complete toolkit for ML and data science

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

[](#installation)

```
composer require phpmlkit/ndarray
```

**Requirements:** PHP 8.1+ with FFI extension enabled

Quick Example
-------------

[](#quick-example)

```
use PhpMlKit\NDArray\NDArray;

// Create arrays
$a = NDArray::array([[1, 2], [3, 4]]);
$b = NDArray::ones([2, 2]);

// Mathematical operations
$c = $a->add($b);
$d = $a->matmul($b);
$e = $a->sum();

// Slicing with zero-copy views
$first_row = $a[0];  // View: no data copied!
$sub_matrix = $a->slice(['0:1', '0:1']);

// Linear algebra
$dot = $a->dot($b);
$trace = $a->trace();
```

Why NDArray?
------------

[](#why-ndarray)

### Zero-Copy Views

[](#zero-copy-views)

Work with large datasets efficiently:

```
$data = NDArray::random([10000, 1000]);  // 80 MB array
$batch = $data->slice(['0:1000']);       // View: 0 bytes copied!
$mean = $batch->mean();                  // Process on view
```

### NumPy Familiarity

[](#numpy-familiarity)

Coming from Python? You'll feel at home (with PHP syntax):

NumPyNDArray PHP`np.array([1, 2, 3])``NDArray::array([1, 2, 3])``arr.shape``$arr->shape()``arr.sum()``$arr->sum()``a + b``$a->add($b)``a[0, 0]``$a['0,0']` or `$a->get(0, 0)``a[0:5]``$a->slice(['0:5'])`Documentation
-------------

[](#documentation)

- [Full Documentation](https://phpmlkit.github.io/ndarray/)
- [Installation Guide](https://phpmlkit.github.io/ndarray/guide/getting-started/installation)
- [Quick Start](https://phpmlkit.github.io/ndarray/guide/getting-started/quick-start)
- [NumPy Migration](https://phpmlkit.github.io/ndarray/guide/getting-started/numpy-migration)
- [API Reference](https://phpmlkit.github.io/ndarray/api)

Key Concepts
------------

[](#key-concepts)

### Views vs Copies

[](#views-vs-copies)

**Views** share memory with the parent array (zero-copy):

```
$arr = NDArray::array([[1, 2], [3, 4]]);
$row = $arr[0];      // View
$row[0] = 999; // Modifies $arr!
```

**Copies** are independent:

```
$copy = $arr->copy();
$copy->set([0, 0], 999);  // Doesn't modify $arr
```

**Operations** create copies:

```
$result = $arr->multiply(2);  // Copy - original unchanged
```

### Important PHP Syntax Differences

[](#important-php-syntax-differences)

PHP does NOT support operator overloading. **Use method calls:**

```
// ❌ This doesn't work in PHP
$c = $a + $b;
$c = $a * 2;

// ✅ Use method calls instead
$c = $a->add($b);
$c = $a->multiply(2);
```

Multi-dimensional indexing uses strings:

```
// ❌ Invalid PHP syntax
$value = $matrix[0, 0];

// ✅ Use string syntax
$value = $matrix['0,0'];
// or
$value = $matrix->get(0, 0);
```

Slicing uses string syntax or method calls:

```
// ❌ Invalid PHP syntax
$slice = $arr[0:5];

// ✅ Use string syntax
$slice = $arr['0:5'];
// or
$slice = $arr->slice(['0:5']);
```

Supported Data Types
--------------------

[](#supported-data-types)

- **Integers:** `Int8`, `Int16`, `Int32`, `Int64`
- **Unsigned:** `UInt8`, `UInt16`, `UInt32`, `UInt64`
- **Floats:** `Float32`, `Float64`
- **Boolean:** `Bool`

Supported Operations
--------------------

[](#supported-operations)

- ✅ Array creation (zeros, ones, random, arange, linspace)
- ✅ Indexing and slicing (multi-dimensional, negative indices, steps)
- ✅ Views and copies (zero-copy slicing, transpose)
- ✅ Arithmetic operations (add, subtract, multiply, divide, mod, power)
- ✅ Mathematical functions (abs, sqrt, exp, log, trig, etc.)
- ✅ Reductions (sum, mean, std, min, max, argmin, argmax)
- ✅ Linear algebra (dot, matmul, trace, diagonal)
- ✅ Shape manipulation (reshape, transpose, squeeze, expandDims)
- ✅ Comparisons and boolean operations
- ✅ Sorting and searching

Development
-----------

[](#development)

```
# Clone repository
git clone https://github.com/phpmlkit/ndarray.git
cd ndarray

# Build the rust library
./scripts/build.sh debug

# Install dependencies
composer install

# Run tests
composer test

# Run static analysis
composer lint

# Format code
composer cs:fix
```

Documentation Development
-------------------------

[](#documentation-development)

```
# Install Node dependencies
npm install

# Start docs dev server
npm run docs:dev

# Build docs
npm run docs:build
```

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

[](#contributing)

Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.

License
-------

[](#license)

MIT License - see [LICENSE](LICENSE) file for details.

Credits
-------

[](#credits)

Created by [CodeWithKyrian](https://github.com/codewithkyrian)

Powered by [Rust ndarray](https://github.com/rust-ndarray/ndarray)

---

⭐ **Star this repo if you find it helpful!**

###  Health Score

45

—

FairBetter than 93% of packages

Maintenance90

Actively maintained with recent releases

Popularity26

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity46

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

Total

2

Last Release

47d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/146c2eb02350558d165083e0018f5377f15f0e71493439c93ee60e7c7ac97fc1?d=identicon)[CodeWithKyrian](/maintainers/CodeWithKyrian)

---

Top Contributors

[![CodeWithKyrian](https://avatars.githubusercontent.com/u/48791154?v=4)](https://github.com/CodeWithKyrian "CodeWithKyrian (101 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/phpmlkit-ndarray/health.svg)

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

###  Alternatives

[codewithkyrian/transformers

State-of-the-art Machine Learning for PHP. Run Transformers in PHP

749231.8k5](/packages/codewithkyrian-transformers)[codewithkyrian/whisper.php

PHP bindings for OpenAI Whisper made possible by whisper.cpp

4046.6k1](/packages/codewithkyrian-whisperphp)[laravel-frontend-presets/zurb-foundation

Laravel 6.0+ front-end preset for Zurb Foundation

5218.7k](/packages/laravel-frontend-presets-zurb-foundation)

PHPackages © 2026

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