Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php // http://stackoverflow.com/questions/29792309/recursively-database-filling-with-multidimensional-array
- // use functions...
- // Now the fun starts...
- //
- // To make the 'tree walk' useful we shall make it call a 'closure' function
- // that has the following signature:
- //
- // function ($childNode, $parentNode);
- //
- // 1) The order that it is called in is not determined
- // 2) Every node will be visited
- //
- // It can be used for any processing you wish.
- // Helper functions for debugging:
- // showNodeProperties($node);
- // -------------------------- Test Data --------------------------
- // Need the ROOT to make the 'tree' complete... !important
- $root = (object) array(
- 'id' => 0,
- 'libelle' => 'Root',
- 'idTypeCategorie' => null,
- 'nomImage' => null,
- 'ponderation' => null,
- 'fils' => null // supplied data goes here.
- );
- // This is actually the $root->fils property...
- $src = array(
- (object) array(
- 'id' => 288,
- 'libelle' => 'Parent: 0 to here: 288',
- 'idTypeCategorie' => 0,
- 'nomImage' => '1905.jpg',
- 'ponderation' => '1',
- 'fils' => array(
- (object) array(
- 'id' => 2228,
- 'libelle' => 'Parent 288 to here: 2228',
- 'idTypeCategorie' => 1,
- 'nomImage' => '2002.png',
- 'ponderation' => 1,
- 'mea' => 2067,
- 'fils' => array(
- (object) array(
- 'id' => 1024,
- 'libelle' => 'Parent 2228 to here: 1024',
- 'idTypeCategorie' => 2,
- 'nomImage' => null,
- 'ponderation' => 1,
- 'nbProduits' => 9,
- 'fils' => Array()
- ),
- (Object) array(
- 'id' => 1025,
- 'libelle' => 'Parent 2228 to here: 1025',
- 'idTypeCategorie' => 2,
- 'nomImage' => null,
- 'ponderation' => 2,
- 'nbProduits' => 10,
- 'fils' => Array()
- )
- ),
- ),
- (object) array(
- 'id' => 2229,
- 'libelle' => 'Parent 288 to here: 2229',
- 'idTypeCategorie' => 1,
- 'nomImage' => '2220.png',
- 'ponderation' => 1,
- 'mea' => 2229,
- 'fils' => array(
- (Object) array(
- 'id' => 1036,
- 'libelle' => 'Parent 2229 to here: 1036',
- 'idTypeCategorie' => 2,
- 'nomImage' => null,
- 'ponderation' => 2,
- 'nbProduits' => 10,
- 'fils' => Array()
- ),
- ),
- ),
- ),
- ),
- (object) array(
- 'id' => 289,
- 'libelle' => 'Parent: 0 to here: 289',
- 'idTypeCategorie' => 0,
- 'nomImage' => '289.jpg',
- 'ponderation' => '1',
- 'fils' => array()
- ),
- );
- // ---- Closure to be called for every node in the tree ----
- // output of the tree walk - The closure MUST USE A REFERENCE to this!
- $outWalkedTree = array();
- $processNode = function ($childNode, $parentNode) use (&$outWalkedTree) {
- $curDetails = array();
- $curDetails['parentId'] = $parentNode->id;
- $curDetails['childId'] = $childNode->id;
- $curDetails['libelle'] = $childNode->libelle;
- $outWalkedTree[] = $curDetails;
- };
- // -------------------------------------------------------------------
- // Make the 'complete tree'... it makes the code easier to understand...
- $sourceTree = $root;
- $sourceTree->fils = $src;
- $EOL = '<br />';
- echo $EOL, ' -------- Source Tree Start ---------';
- echo '<pre>';
- print_r($sourceTree);
- echo '</pre>';
- echo $EOL, ' -------- Source Tree End ---------';
- echo $EOL, ' -------- Tree walk Start ---------';
- echo $EOL, 'walkTree($sourceTree, null, $processNode);';
- walkTree($sourceTree, null, $processNode);
- echo $EOL, ' -------- Tree walk End ---------';
- echo $EOL, ' -------- outWalkedTree Start ---------';
- echo '<pre>';
- print_r($outWalkedTree);
- echo '</pre>';
- echo $EOL, ' -------- outWalkedTree End ---------';
- exit(); // redundant but a clue when reading where the main processing ends
- // ------------------- Tree processing functions ------------------------
- /**
- * Apply processing to the current node and it's parent node.
- *
- * @param node $currentNode
- * @param node $parentNode
- * @param closure $processNode
- * @return void This is all 'side effects'
- */
- function walkTree($currentNode, $parentNode, /* closure */ $processNode)
- {
- // call the closure - but only if the parent is set...
- if (is_object($parentNode)) {
- $processNode($currentNode, $parentNode);
- }
- // process the children of this node
- foreach ($currentNode->fils as $childNode) {
- walkTree($childNode, $currentNode, $processNode); // recurse for every node
- }
- return;
- }
- // ------------------ Helpers -----------------------
- function showNodeProperties($node, $title = '')
- {
- $EOL = '<br />';
- echo $EOL, ' -------- Node '. $title .' (id: '. $node->id .') details ---------';
- foreach((array) $node as $property => $value) {
- $propValue = $value;
- if ($property == 'fils') {
- $propValue = 'Child Count: '. count($node->fils);
- }
- echo $EOL, 'Node: key: '. $property .' =>'. $propValue;
- }
- echo $EOL, ' -------- end '. $title .' (id: '. $node->id .') end ---------';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement