Matrix 1.3.0 · Library for PHP
Spreadsheet logic, in PHP
Matrix keeps data in rows and columns, and a cell can hold a function instead of a value. Change a value and the cells that depend on it are calculated again, even in other matrices. Then export the table as Markdown, JSON, YAML, CSV, XML or HTML.
Free software (GPL 3) · PHP 8.0 or newer · one class, no dependencies
- You changeApple price: 10 → 20
- RecalculatedApple amount: 20 → 40
- RecalculatedTotal: 185 → 205
Examples
What sets it apart
Each example is a complete script, run with Matrix 1.3.0. The output is what it printed.
Cells that calculate
A cell holds a function of its row, its column and the matrix. Change a price, and the amount and the total follow.
<?php require __DIR__ . '/../vendor/autoload.php'; use divengine\matrix; // A cell can hold a function of its row, its column and the matrix $amount = fn ($row, $col, matrix $m) => $m->get($row, 1) * $m->get($row, 2); $total = fn ($row, $col, matrix $m) => array_sum($m->vertical($col, 1, $row - 1)); $order = new matrix([ ['Product', 'Price', 'Qty', 'Amount'], ['Apple', 10, 2, $amount], ['Banana', 35, 3, $amount], ['Orange', 6, 10, $amount], ['Total', '', '', $total], ]); echo $order; // Change one price: the amount and the total follow $order->{'1.1'} = 20; echo "\nApple now costs 20:\n"; echo $order;
Product Price Qty Amount Apple 10 2 20 Banana 35 3 105 Orange 6 10 60 Total 185 Apple now costs 20: Product Price Qty Amount Apple 20 2 40 Banana 35 3 105 Orange 6 10 60 Total 205
Matrices that depend on each other
A quote reads its prices from another matrix. Setting a price there calculates the quote again.
<?php require __DIR__ . '/../vendor/autoload.php'; use divengine\matrix; // Two matrices: prices, and a quote whose cells read the prices $prices = new matrix([ ['Item', 'Unit price'], ['Hour', 50], ['Visit', 80], ]); $quote = new matrix([ ['Item', 'Qty', 'Cost'], ['Hour', 6, fn ($r, $c, matrix $m) => $m->get($r, 1) * $prices->get(1, 1)], ['Visit', 2, fn ($r, $c, matrix $m) => $m->get($r, 1) * $prices->get(2, 1)], ]); echo $quote; // Raise the hourly price in the other matrix: the quote is recalculated $prices->set(1, 1, 60); echo "\nThe hour now costs 60:\n"; echo $quote;
Item Qty Cost Hour 6 300 Visit 2 160 The hour now costs 60: Item Qty Cost Hour 6 360 Visit 2 160
One table, many formats
With the first row as column names, the same matrix as Markdown, JSON objects and YAML. Text, CSV, XML and HTML work the same way.
<?php require __DIR__ . '/../vendor/autoload.php'; use divengine\matrix; $team = new matrix([ ['Name', 'Role', 'Since'], ['Ada', 'Engineer', 2019], ['Grace', 'Architect', 2016], ]); // true: the first row holds the column names echo $team->formatMarkdown(true), "\n"; echo $team->formatJSON(true), "\n\n"; echo $team->formatYAML(true);
| Name | Role | Since |
| --- | --- | --- |
| Ada | Engineer | 2019 |
| Grace | Architect | 2016 |
[{"name":"Ada","role":"Engineer","since":2019},{"name":"Grace","role":"Architect","since":2016}]
fields:
- name
- role
- since
data:
- name: Ada
role: Engineer
since: 2019
- name: Grace
role: Architect
since: 2016Install
Add it to a project
1. Require it
With Composer.
composer require divengine/matrix:1.3.0
2. Run an example
Save one of the scripts above in an examples folder of the project, then:
php examples/formulas.php
The examples, with the commands to run them again with Matrix 1.3.0, are in the site's repository. The library has more in its own examples folder.