Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php // http://stackoverflow.com/questions/30299576/build-paths-of-all-paths-of-a-tree-structured-array-in-php
- $tree = Array(
- 0 => Array(
- "name" => 'Furniture',
- "slug" => 'furniture',
- "children" => Array(
- "0" => Array
- (
- "name" => 'Sofa',
- "slug" => 'sofa',
- "leafs" => Array
- (
- "0" => Array ( "name" => '3 Seater', "slug" => '3-seater'),
- "1" => Array ( "name" => '4 Seater', "slug" => '4-seater'),
- ),
- ),
- "1" => Array
- (
- "name" => 'Chairs',
- "slug" => 'chairs',
- "leafs" => Array
- (
- "0" => Array ( "name" => '3 Seater', "slug" => '3-seater'),
- "1" => Array ( "name" => '4 Seater', "slug" => '4-seater'),
- )
- )
- )
- ),
- 1 => Array(
- "name" => 'Furniture1',
- "slug" => 'furniture1',
- "children" => Array(
- "0" => Array(
- "name" => 'Sofa1',
- "slug" => 'sofa1',
- "leafs" => Array(
- "0" => Array ( "name" => '3 Seater1', "slug" => '3-seater1'),
- "1" => Array ( "name" => '4 Seater1', "slug" => '4-seater1'),
- )
- ),
- "1" => Array(
- "name" => 'Chairs1',
- "slug" => 'chairs1',
- "leafs" => Array(
- "0" => Array ( "name" => '3 Seater1', "slug" => '3-seater1'),
- "1" => Array ( "name" => '4 Seater1', "slug" => '4-seater1')
- )
- )
- )
- )
- );
- echo '<pre>';
- print_r($tree);
- echo '</pre>';
- /* --------------------------------------------------------------------------
- * Run the program
- */
- // This really should be class. Later...
- /* -------------------------------------
- * Store all the paths in here...
- */
- $allPaths = array();
- /* -------------------------------------
- * The 'current' path to the leaf node
- *
- * This is a 'stack'. The 'path' is all the entries combined.
- *
- * @var array
- */
- $currentPath = array(); // this is a 'stack'
- walkTree($allPaths, // reference
- $currentPath, // reference
- $tree,
- 'children',
- /**
- * The closure that is run for each 'leaf' node only currently
- * It could be called on every node if required.
- *
- * @param array currentPath to this value
- * @param array node - the actual leaf data
- * @param string nodeType - leaf or children
- */
- function(array $currentPath,
- $node,
- $nodeType = 'leaf') {
- $fullPath = '';
- // var_dump(__FILE__.__LINE__, 'closure start:', $node);
- if ($nodeType === 'leaf') {
- $fullPath = 'path: '. implode(' / ', $currentPath)
- .' item: '. $node['name'];
- }
- return $fullPath;
- }
- );
- var_dump(__FILE__.__LINE__, $allPaths);
- // ----------------------------- end of processing ---------------------------
- exit;
- /*
- * This takes the entire `current tree state` and decides what to do with the current node.
- */
- /**
- * Apply processing to the current node.
- *
- * @param array $allPaths -- added to by the 'processNode' closure
- * @param array $currentPath -- the full path to the current data
- * @param array $nodeList -- all the descendants from this node
- * @param string $nodeListType 'children' or 'leaf'
- * @param closure $processNode -- called to process the current node data
- * -- needs the full path to the current node
- * @return void This is all 'side effects'
- */
- function walkTree(&$allPaths,
- &$currentPath,
- $nodeList,
- $nodeListType,
- /* closure */ $processNode)
- {
- // var_dump(__FILE__.__LINE__,
- // $currentPath,
- // $nodeList,
- // $nodeListType
- // );
- foreach ($nodeList as $key => $nodeValue) {
- if ($nodeListType === 'leaf') {
- $allPaths[] = $processNode($currentPath, $nodeValue, $nodeListType);
- continue; // will not recurse again from here.
- }
- // this must happen always...
- // var_dump('before processing one node: pushed: '. $nodeValue['name'], $currentPath);
- array_push($currentPath, $nodeValue['name']);
- if (isset($nodeValue['children'])) {
- $nodeType = 'children';
- $nodeList = $nodeValue['children'];
- }
- else {
- $nodeType = 'leaf';
- $nodeList = $nodeValue['leafs'];
- }
- walkTree($allPaths,
- $currentPath,
- $nodeList,
- $nodeType,
- $processNode);
- $name = array_pop($currentPath);
- // var_dump('after processing one node: popped: '. $name, $currentPath);
- } // end of current list
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement