Advertisement
rfv123

stackoverflow.com recursively filling multidimensional array

Apr 23rd, 2015
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.75 KB | None | 0 0
  1. <?php // http://stackoverflow.com/questions/29792309/recursively-database-filling-with-multidimensional-array
  2. //  use functions...
  3.  
  4. // Now the fun starts...
  5. //
  6. // To make the 'tree walk' useful we shall make it call a 'closure' function
  7. // that has the following signature:
  8. //
  9. // function ($childNode, $parentNode);
  10. //
  11. // 1) The order that it is called in is not determined
  12. // 2) Every node will be visited
  13. //
  14. // It can be used for any processing you wish.
  15.  
  16. // Helper functions for debugging:
  17. //   showNodeProperties($node);
  18.  
  19. // -------------------------- Test Data --------------------------
  20. // Need the ROOT to make the 'tree' complete... !important
  21. $root = (object) array(
  22.             'id' => 0,
  23.             'libelle' => 'Root',
  24.             'idTypeCategorie' => null,
  25.             'nomImage' => null,
  26.             'ponderation' => null,
  27.             'fils' => null // supplied data goes here.
  28.        );
  29.  
  30. // This is actually the  $root->fils property...
  31. $src = array(
  32.          (object) array(
  33.             'id' => 288,
  34.             'libelle' => 'Parent: 0 to here: 288',
  35.             'idTypeCategorie' => 0,
  36.             'nomImage' => '1905.jpg',
  37.             'ponderation' => '1',
  38.             'fils' => array(
  39.                             (object) array(
  40.                                 'id' => 2228,
  41.                                 'libelle' => 'Parent 288 to here: 2228',
  42.                                 'idTypeCategorie' => 1,
  43.                                 'nomImage' => '2002.png',
  44.                                 'ponderation' => 1,
  45.                                 'mea' => 2067,
  46.                                 'fils' => array(
  47.                                         (object) array(
  48.                                             'id' => 1024,
  49.                                             'libelle' => 'Parent 2228 to here: 1024',
  50.                                             'idTypeCategorie' => 2,
  51.                                             'nomImage' => null,
  52.                                             'ponderation' => 1,
  53.                                             'nbProduits' => 9,
  54.                                             'fils' =>  Array()
  55.                                         ),
  56.                                         (Object) array(
  57.                                             'id' => 1025,
  58.                                             'libelle' => 'Parent 2228 to here: 1025',
  59.                                             'idTypeCategorie' => 2,
  60.                                             'nomImage' => null,
  61.                                             'ponderation' => 2,
  62.                                             'nbProduits' => 10,
  63.                                             'fils' => Array()
  64.                                         )
  65.                                     ),
  66.                              ),
  67.                              (object) array(
  68.                                 'id' => 2229,
  69.                                 'libelle' => 'Parent 288 to here: 2229',
  70.                                 'idTypeCategorie' => 1,
  71.                                 'nomImage' => '2220.png',
  72.                                 'ponderation' => 1,
  73.                                 'mea' => 2229,
  74.                                 'fils' => array(
  75.                                          (Object) array(
  76.                                             'id' => 1036,
  77.                                             'libelle' => 'Parent 2229 to here: 1036',
  78.                                             'idTypeCategorie' => 2,
  79.                                             'nomImage' => null,
  80.                                             'ponderation' => 2,
  81.                                             'nbProduits' => 10,
  82.                                             'fils' => Array()
  83.                                         ),
  84.                                   ),
  85.                             ),
  86.                         ),
  87.  
  88.              ),
  89.              (object) array(
  90.                 'id' => 289,
  91.                 'libelle' => 'Parent: 0 to here: 289',
  92.                 'idTypeCategorie' => 0,
  93.                 'nomImage' => '289.jpg',
  94.                 'ponderation' => '1',
  95.                 'fils' => array()
  96.              ),
  97.    );
  98.  
  99. // ---- Closure to be called for every node in the tree ----
  100.  
  101. // output of the tree walk - The closure MUST USE A REFERENCE to this!
  102. $outWalkedTree = array();
  103.  
  104. $processNode  = function ($childNode, $parentNode) use (&$outWalkedTree) {
  105.  
  106.     $curDetails = array();
  107.     $curDetails['parentId'] = $parentNode->id;
  108.     $curDetails['childId'] = $childNode->id;
  109.     $curDetails['libelle'] = $childNode->libelle;
  110.  
  111.     $outWalkedTree[] = $curDetails;
  112. };
  113. // -------------------------------------------------------------------
  114.  
  115. // Make the 'complete tree'... it makes the code easier to understand...
  116. $sourceTree = $root;
  117. $sourceTree->fils = $src;
  118.  
  119. $EOL = '<br />';
  120.  
  121. echo $EOL, '  -------- Source Tree Start ---------';
  122. echo '<pre>';
  123. print_r($sourceTree);
  124. echo '</pre>';
  125. echo $EOL, '  -------- Source Tree End ---------';
  126.  
  127.  
  128. echo $EOL, '  -------- Tree walk Start ---------';
  129. echo $EOL, 'walkTree($sourceTree, null, $processNode);';
  130. walkTree($sourceTree, null, $processNode);
  131. echo $EOL, '  -------- Tree walk  End ---------';
  132.  
  133. echo $EOL, '  -------- outWalkedTree Start ---------';
  134. echo '<pre>';
  135. print_r($outWalkedTree);
  136. echo '</pre>';
  137. echo $EOL, '  -------- outWalkedTree End ---------';
  138.  
  139. exit(); // redundant but a clue when reading where the main processing ends
  140.  
  141. // ------------------- Tree processing functions ------------------------
  142.  
  143. /**
  144.  * Apply processing to the current node and it's parent node.
  145.  *
  146.  * @param node $currentNode
  147.  * @param node $parentNode
  148.  * @param closure $processNode
  149.  * @return void This is all 'side effects'
  150.  */
  151. function walkTree($currentNode, $parentNode, /* closure */ $processNode)
  152. {
  153.     // call the closure - but only if the parent is set...
  154.     if (is_object($parentNode)) {
  155.         $processNode($currentNode, $parentNode);
  156.     }
  157.  
  158.     // process the children of this node
  159.     foreach ($currentNode->fils as $childNode) {
  160.  
  161.         walkTree($childNode, $currentNode, $processNode); // recurse for every node
  162.     }
  163.     return;
  164. }
  165.  
  166.  
  167. // ------------------ Helpers -----------------------
  168. function showNodeProperties($node, $title = '')
  169. {
  170.     $EOL = '<br />';
  171.     echo $EOL, '  -------- Node '.  $title .' (id: '. $node->id .') details ---------';
  172.     foreach((array) $node as $property => $value) {
  173.  
  174.         $propValue = $value;
  175.         if ($property == 'fils') {
  176.              $propValue = 'Child Count: '. count($node->fils);
  177.         }
  178.         echo $EOL, 'Node: key: '. $property .' =>'. $propValue;
  179.     }
  180.     echo $EOL, '  -------- end '.  $title .' (id: '. $node->id .') end ---------';
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement