PHPackages                             stefangabos/zebra\_mptt - 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. stefangabos/zebra\_mptt

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

stefangabos/zebra\_mptt
=======================

A PHP library providing an implementation of the modified preorder tree traversal algorithm

2.3.7(4y ago)7654228[1 PRs](https://github.com/stefangabos/Zebra_Mptt/pulls)LGPL-3.0PHPPHP &gt;=5.0.0

Since Sep 14Pushed 3y ago10 watchersCompare

[ Source](https://github.com/stefangabos/Zebra_Mptt)[ Packagist](https://packagist.org/packages/stefangabos/zebra_mptt)[ Docs](https://github.com/stefangabos/Zebra_Mptt)[ GitHub Sponsors](https://github.com/stefangabos)[ RSS](/packages/stefangabos-zebra-mptt/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (10)DependenciesVersions (11)Used By (0)

[![zebrajs](https://github.com/stefangabos/zebrajs/raw/master/docs/images/logo.png)](https://github.com/stefangabos/zebrajs/blob/master/docs/images/logo.png)

Zebra Mptt [![Tweet](https://camo.githubusercontent.com/cb820a0ecc9645168e33b03925d7f14691262ddbaeaf66a0a91697803d0cba2d/68747470733a2f2f696d672e736869656c64732e696f2f747769747465722f75726c2f687474702f736869656c64732e696f2e7376673f7374796c653d736f6369616c)](https://twitter.com/intent/tweet?text=A+PHP+library+providing+an+implementation+of+the+modified+preorder+tree+traversal+(MPTT)+algorithm&url=https://github.com/stefangabos/Zebra_Mptt&via=stefangabos&hashtags=php,mptt)
===========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#zebra-mptt-)

*A PHP library providing an implementation of the modified preorder tree traversal (MPTT) algorithm*

[![Latest Stable Version](https://camo.githubusercontent.com/9491d136b51ccb462a285ef9aa13496526a5769d8c307b6be1361a09bef1b1d5/68747470733a2f2f706f7365722e707567782e6f72672f73746566616e6761626f732f7a656272615f6d7074742f762f737461626c65)](https://packagist.org/packages/stefangabos/zebra_mptt) [![Total Downloads](https://camo.githubusercontent.com/50bfb5f1ef69fb4884408457af695e556cc978d838a9369ca7fe3f69e0d02650/68747470733a2f2f706f7365722e707567782e6f72672f73746566616e6761626f732f7a656272615f6d7074742f646f776e6c6f616473)](https://packagist.org/packages/stefangabos/zebra_mptt) [![Monthly Downloads](https://camo.githubusercontent.com/12fa8f9aa8f4cf676dc99d222eee4ff9658d4602bff6b85f5fd864601d80b34f/68747470733a2f2f706f7365722e707567782e6f72672f73746566616e6761626f732f7a656272615f6d7074742f642f6d6f6e74686c79)](https://packagist.org/packages/stefangabos/zebra_mptt) [![Daily Downloads](https://camo.githubusercontent.com/5625c9c6649ec4e9504a0cb828a97b4e42dd9fd169b559260c77f6288b50e35a/68747470733a2f2f706f7365722e707567782e6f72672f73746566616e6761626f732f7a656272615f6d7074742f642f6461696c79)](https://packagist.org/packages/stefangabos/zebra_mptt) [![License](https://camo.githubusercontent.com/b4746648f7ccf6cccaec41c1554aa27e9e6a488acd814fd3acc14fe5d6963073/68747470733a2f2f706f7365722e707567782e6f72672f73746566616e6761626f732f7a656272615f6d7074742f6c6963656e7365)](https://packagist.org/packages/stefangabos/zebra_mptt)

What is Modified Preorder Tree Traversal
----------------------------------------

[](#what-is-modified-preorder-tree-traversal)

MPTT is a fast algorithm for storing hierarchical data (like categories and their subcategories) in a relational database. This is a problem that most of us have had to deal with, and for which we've used an [adjacency list](http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/), where each item in the table contains a pointer to its parent and where performance will naturally degrade with each level added as more queries need to be run in order to fetch a subtree of records.

The aim of the modified preorder tree traversal algorithm is to make retrieval operations very efficient. With it you can fetch an arbitrary subtree from the database using just two queries. The first one is for fetching details for the root node of the subtree, while the second one is for fetching all the children and grandchildren of the root node.

The tradeoff for this efficiency is that updating, deleting and inserting records is more expensive, as there's some extra work required to keep the tree structure in a good state at all times. Also, the modified preorder tree traversal approach is less intuitive than the adjacency list approach because of its algorithmic flavour.

For more information about the modified preorder tree traversal method, read this excellent article called [Storing Hierarchical Data in a Database](https://www.sitepoint.com/hierarchical-data-database/).

What is Zebra Mptt
------------------

[](#what-is-zebra-mptt)

**Zebra Mptt** is a PHP library that provides an implementation of the modified preorder tree traversal algorithm making it easy to implement the MPTT algorithm in your PHP applications.

It provides methods for adding nodes anywhere in the tree, deleting nodes, moving and copying nodes around the tree and methods for retrieving various information about the nodes.

Zebra Mptt uses [table locks](http://dev.mysql.com/doc/refman/8.0/en/lock-tables.html) making sure that database integrity is always preserved and that concurrent MySQL sessions don't compromise data integrity. Also, Zebra Mptt uses a caching mechanism which has as result the fact that regardless of the type, or the number of retrieval operations, **the database is read only once per script execution!**

Features
--------

[](#features)

- provides methods for adding nodes anywhere in the tree, deleting nodes, moving and copying nodes around the tree and methods for retrieving various information about the nodes
- uses a caching mechanism which has as result the fact that regardless of the type, or the number of retrieval operations, **the database is read only once per script execution**
- uses [table locks](http://dev.mysql.com/doc/refman/8.0/en/lock-tables.html) making sure that database integrity is always preserved and that concurrent MySQL sessions don't compromise data integrity
- uses **mysqli** extension
- has [awesome documentation](https://stefangabos.github.io/Zebra_Mptt/Zebra_Mptt/Zebra_Mptt.html)
- code is heavily commented and generates no warnings/errors/notices when PHP's error reporting level is set to [E\_ALL](https://www.php.net/manual/en/function.error-reporting.php)

📔 Documentation
---------------

[](#notebook_with_decorative_cover-documentation)

Check out the [awesome documentation](https://stefangabos.github.io/Zebra_Mptt/Zebra_Mptt/Zebra_Mptt.html)!

🎂 Support the development of this project
-----------------------------------------

[](#-support-the-development-of-this-project)

Your support is greatly appreciated and it keeps me motivated continue working on open source projects. If you enjoy this project please star it by clicking on the star button at the top of the page. If you're feeling generous, you can also buy me a coffee through PayPal or become a sponsor. **Thank you for your support!** 🎉

[![Star it on GitHub](https://camo.githubusercontent.com/98c4828823f0be3b8908d1b1cc9642e88e167f0a50e713c834352113a1cb2674/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f73746566616e6761626f732f7a656272615f6d7074743f636f6c6f723d677265656e266c6162656c3d7374617225323069742532306f6e253230476974487562)](https://github.com/stefangabos/Zebra_Mptt) [![Donate](https://camo.githubusercontent.com/604e3db9c8751116b3f765aad0353ec7ded655bbe8aaacbc38d8c4a6b784b3ed/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f446f6e6174652d50617950616c2d677265656e2e737667)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=49VT6G7L5GPAS) [![](https://camo.githubusercontent.com/7a6725dd6857bfabaca4b6783ac9ff3c2e882456681b5fdfb1fae916c0461c73/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f2d53706f6e736f722d6661666266633f6c6f676f3d47697448756225323053706f6e736f7273)](https://github.com/sponsors/stefangabos)

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

[](#requirements)

PHP 5.0.0+, MySQL 4.1.22+

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

[](#installation)

You can install Zebra Mptt via [Composer](https://packagist.org/packages/stefangabos/zebra_mptt)

```
# get the latest stable release
composer require stefangabos/zebra_mptt

# get the latest commit
composer require stefangabos/zebra_mptt:dev-master
```

Or you can install it manually by downloading the latest version, unpacking it, and then including it in your project

```
require_once 'path/to/Zebra_Mptt.php';
```

Install MySQL table
-------------------

[](#install-mysql-table)

Notice a directory called *install* containing a file named *mptt.sql*. This file contains the SQL code that will create the table used by the class to store its data. Import or execute the SQL code using your preferred MySQL manager (like phpMyAdmin or the fantastic Adminer) into a database of your choice.

How to use
----------

[](#how-to-use)

```
// include the Zebra_Mptt class
// (you don't need this if you are using Composer)
require 'path/to/Zebra_Mptt.php';

// instantiate a new object
$mptt = new Zebra_Mptt();

// populate the table

// add 'Food' as a topmost node
$food = $mptt->add(0, 'Food');

// 'Fruit' and 'Meat' are direct descendants of 'Food'
$fruit = $mptt->add($food, 'Fruit');
$meat = $mptt->add($food, 'Meat');

// 'Red' and 'Yellow' are direct descendants of 'Fruit'
$red = $mptt->add($fruit, 'Red');
$yellow = $mptt->add($fruit, 'Yellow');

// add a fruit of each color
$cherry = $mptt->add($red, 'Cherry');
$banana = $mptt->add($yellow, 'Banana');

// add two kinds of meat
$mptt->add($meat, 'Beef');
$mptt->add($meat, 'Pork');

// move 'Banana' to 'Meat'
$mptt->move($banana, $meat);

// get a flat array of descendants of 'Meat'
$mptt->get_descendants($meat);

// get a multidimensional array (a tree) of all the data in the database
$mptt->get_tree();
```

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 97.1% 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 ~349 days

Recently: every ~453 days

Total

10

Last Release

1483d ago

PHP version history (3 changes)2.2.4PHP &gt;=4.4.9

2.3.0PHP &gt;=5.0.5

2.3.1PHP &gt;=5.0.0

### Community

Maintainers

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

---

Top Contributors

[![stefangabos](https://avatars.githubusercontent.com/u/741165?v=4)](https://github.com/stefangabos "stefangabos (101 commits)")[![dominikfiala](https://avatars.githubusercontent.com/u/3163672?v=4)](https://github.com/dominikfiala "dominikfiala (1 commits)")[![erlingormar](https://avatars.githubusercontent.com/u/738922?v=4)](https://github.com/erlingormar "erlingormar (1 commits)")[![JamesShaver](https://avatars.githubusercontent.com/u/746553?v=4)](https://github.com/JamesShaver "JamesShaver (1 commits)")

---

Tags

algorithmsmpttphpphp-mptttree-traversalphptreeMPTTtraversalmodifiedpreorder

### Embed Badge

![Health badge](/badges/stefangabos-zebra-mptt/health.svg)

```
[![Health](https://phpackages.com/badges/stefangabos-zebra-mptt/health.svg)](https://phpackages.com/packages/stefangabos-zebra-mptt)
```

###  Alternatives

[loophp/phptree

An implementation of tree data structure

981.8M2](/packages/loophp-phptree)[efureev/laravel-trees

Multi-Tree structures for Laravel

14253.3k4](/packages/efureev-laravel-trees)[chdemko/sorted-collections

Sorted Collections for PHP &gt;= 8.2

222.5M3](/packages/chdemko-sorted-collections)

PHPackages © 2026

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