Skip to content

Instantly share code, notes, and snippets.

@Sumurai8
Sumurai8 / with_descendants_and_children.php
Last active February 5, 2018 22:12
Add "descendants" and "children" property to collection of nodes in nested set format
<?php
/**
* Adds the children and descendants attribute to an ordered set of nodes
*
* @param array $nodes pre-order walk of nodes in nested set format
*/
function with_descendants_and_children(array $nodes) {
// First we initialise our stack
$ancestor_stack = [];
@Sumurai8
Sumurai8 / with_children_generator.php
Created February 3, 2018 14:46
Add "children" property to collection of nodes in nested set format
<?php
/**
* Simply returns the titles of the nodes passed to it
*
* @param array $nodes Collection of nodes we want to have the titles of
*/
function titles(array $nodes) {
return array_map(
function ($node) {
return $node['title'];
@Sumurai8
Sumurai8 / with_descendants_generator.php
Created February 3, 2018 12:29
Add "descendants" property to collection of nodes in nested set format with a generator
<?php
/**
* Simply returns the titles of the nodes passed to it
*
* @param array $nodes Collection of nodes we want to have the titles of
*/
function titles(array $nodes) {
return array_map(
function ($node) {
return $node['title'];
@Sumurai8
Sumurai8 / with_ancestors_generator.php
Created February 3, 2018 09:40
Add "ancestor" property to collection of nodes in nested set format
<?php
/**
* Simply returns the titles of the nodes passed to it
*
* @param array $nodes Collection of nodes we want to have the titles of
*/
function titles(array $nodes) {
return array_map(
function ($node) {