All pastes #192761 Raw Edit

Untitled

public text v1 · immutable
·published 2006-10-06 06:24 UTC
rendered paste body
<?php
/**
* Date    =  2006-03-23
**/

class Tree {

  /**
  * @desc array with elements
  * @type array
  * @access private
  **/
  private $aElements;

  /**
  * @desc backbone array
  * @type array
  * @access private
  **/
  private $result;
  
  /**
  * @desc constructor method
  * @param
  * @access public
  * @return void
  **/
  public function __construct(){
    $this->aElements  = array();
    $this->result     = array();
  }

  /**
  * @desc add a node to the tree
  * @param integer, array, integer
  * @access public
  * @return void
  **/
  public function addNode($iId, $sName, $iParentId = 0){
    $this->aElements[$iParentId][] = array($iId, $sName);
  }

  /**
  * @desc add a subnode to the tree
  * @param integer, integer
  * @access private
  * @return void
  **/  
  private function addSubNode($iParentId, $iLevel){
    if(isset($this->aElements[$iParentId])){
      $iElements = count($this->aElements[$iParentId]);
      
      for($i = 0; $i < $iElements; $i++){
        $this->result[] = $this->aElements[$iParentId][$i][1];
        $this->addSubNode($this->aElements[$iParentId][$i][0], $iLevel+1);
      }
    }
  }

  /**
  * @desc return the array as an associative array
  * @param 
  * @access public
  * @return array
  **/  
  public function getArray() {
    if(isset($this->aElements[0])) {
      $iElements = count($this->aElements[0]);
    
      for($i=0; $i < $iElements; $i++){
        $this->result[] = $this->aElements[0][$i][1];
        $this->addSubNode($this->aElements[0][$i][0], 1);
      }
    }
    return $this->result;
  }
}
?>