Advertisement
cecepsuwanda

merge

Oct 24th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.86 KB | None | 0 0
  1. <?php
  2.  
  3.    function addchild(&$tree,$prefix,$lvl,$idx){
  4.      
  5.       if($idx<strlen($lvl)){
  6.            
  7.            if(!isset($tree[$prefix.'_'.substr($lvl,0,$idx+1)])){
  8.              $tree[$prefix.'_'.substr($lvl,0,$idx+1)] = array();  
  9.            }
  10.            addchild($tree[$prefix.'_'.substr($lvl,0,$idx+1)],$prefix,$lvl,$idx+1);
  11.       }    
  12.    }
  13.  
  14.  
  15. $str1 = "Node_1.Node_11.Node_111.Node_1111.Node_11111";
  16. $str2 = "Node_1.Node_12.Node_1121.Node_11211.Node_12111";
  17. $str3 = "Node_1.Node_12.Node_123.Node_1231.Node_12311";
  18. $str4 = "Node_2.Node_21.Node_211.Node_2111.Node_21111";
  19. $all = $str1.'|'.$str2.'|'.$str3.'|'.$str4;
  20.  
  21. $tree=array();
  22.  
  23. $lvl1 = explode('|',$all);
  24.  
  25. foreach($lvl1 as $str)
  26. {
  27.     $lvl2 = explode('.',$str);
  28.     foreach($lvl2 as $node)
  29.     {
  30.       $temp = explode('_',$node);
  31.       addchild($tree,$temp[0],$temp[1],0);  
  32.        
  33.     }
  34.    
  35. }
  36. echo '<pre>';
  37. print_r($tree);
  38. echo '</pre>';
  39.  
  40. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement