PHPackages                             kelemen/recursor - 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. kelemen/recursor

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

kelemen/recursor
================

Create recursive array from one level array or iterable object

v1.0.0(11y ago)06.5k↓33.3%[2 issues](https://github.com/ricco24/recursor/issues)MITPHPPHP &gt;=5.5.0

Since Apr 7Pushed 11y ago2 watchersCompare

[ Source](https://github.com/ricco24/recursor)[ Packagist](https://packagist.org/packages/kelemen/recursor)[ RSS](/packages/kelemen-recursor/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

Recursor
========

[](#recursor)

Library build recursive array from one level array with some recursive dependency.

\####Input array example

```
$categories = [
	1 => [
		'title' => 'Sport',
		'portal' => 'TopSport',
		'parent_id' => null,
	],
	2 => [
		'title' => 'Hockey',
		'portal' => 'TopSport',
		'parent_id' => 1,
	],
	3 => [
		'title' => 'Football',
		'portal' => 'TopSport',
		'parent_id' => 1,
	],
	4 => [
		'title' => 'World',
		'portal' => 'BestNews',
		'parent_id' => null,
	]
];
```

NodeListRecursor
----------------

[](#nodelistrecursor)

Only lists (elements without children) has title, nodes are arrays with children.

```
use Kelemen\Recursor\NodeListRecursor;

$recursor = new NodeListRecursor($categories, 'title', 'parent_id');
# sets maximal recursion depth
$list->setMaxDepth(3);
# get final recursive array
$list = $recursor->getList();

# result
array(2) {
  [1] => array(2) {
    [2] => string(6) "Hockey"
    [3] => string(8) "Football"
  }
  [4] => string(5) "World"
}
```

GroupRecursor
-------------

[](#grouprecursor)

Very usefull to generate array for form selects.

```
use Kelemen\Recursor\GroupRecursor;

$recursor = new GroupRecursor($categories, 'portal', 'title', 'parent_id');
$list = $recursor->getList();

# result
array(2) {
  ["TopSport"] => array(3) {
    [1] => string(5) "Sport"
    [2] => string(7) "- Hockey"
    [3] => string(9) "- Football"
  }
  ["BestNews"] => array(1) {
    [4] => string(5) "World"
  }
}
```

Custom recursor
---------------

[](#custom-recursor)

You can create recursor that fits exact you needs. You need to implement 3 abstract methods.

```
# Used as item key in result array
abstract protected function getItemKey($item, $key);

# Get parent key for given item
abstract protected function getParentItemKey($item, $key);

# Returns result item structure as array
abstract protected function getItemData($item, $key, $childrenCount, $level);
```

Every recursor can use 2 hook methods

```
# Called after build every item
protected function afterItemHook(&$item);

# Called after build result array
protected function afterAllHook(&$items);
```

If you want to create custom recursor with children in result you need to define **$subKey** class parameter. This param needs to be used in **getItemData()** method as array.

### Example of custom recursor

[](#example-of-custom-recursor)

This recursor counts recursive item children.

```
namespace Kelemen\Recursor;

class CategoryRecursor extends Recursor
{
	/** @var string			Sub key */
	protected $subKey = 'sub';

	/**
	 * Returns item key
	 * @param $item
	 * @param $key
	 * @return mixed
	 */
	protected function getItemKey($item, $key)
	{
		return $item['id'];
	}

	/**
	 * Returns item parent key
	 * @param $item
	 * @param $key
	 * @return mixed
	 */
	protected function getParentItemKey($item, $key)
	{
		return $item['parent_id'];
	}

	/**
	 * Returns item result data
	 * @param $item
	 * @param $key
	 * @param int $childrenCount
	 * @param int $level
	 * @return array
	 */
	protected function getItemData($item, $key, $childrenCount, $level)
	{
		return [
			'title' => $item['title'],
			'portal_id' => $item['portal_id'],
			'count' => 0,
			$this->subKey => []
		];
	}

	/**
	 * @param array $return
	 */
	public function afterItemHook(&$return)
	{
		$count = count($return['sub']);
		foreach ($return['sub'] as $sub) {
			$count += $sub['count'];
		}
		$return['count'] = $count;
	}
}
```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity58

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

Unknown

Total

1

Last Release

4060d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/72f58e3d494056872274a7cd14323f13cc76e52c966eade5dec3129ea378c182?d=identicon)[kelemen.samuel](/maintainers/kelemen.samuel)

---

Top Contributors

[![ricco24](https://avatars.githubusercontent.com/u/1409647?v=4)](https://github.com/ricco24 "ricco24 (3 commits)")

### Embed Badge

![Health badge](/badges/kelemen-recursor/health.svg)

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

###  Alternatives

[devgeniem/wp-no-admin-ajax

A WordPress plugin that lightens the WP AJAX routine and directs the requests to front-end rather than admin back-end.

3821.4k1](/packages/devgeniem-wp-no-admin-ajax)[0x13a/geodistance-php

Calculate geodistance between two points, latitude and longitude

3120.6k](/packages/0x13a-geodistance-php)

PHPackages © 2026

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