簡體   English   中英

PHP:以樹形結構保存數據

[英]PHP: Save data in tree structure

我有一個看起來很不錯的Excel:

在此處輸入圖片說明

如果我讀了它(編輯:我使用PHPExcel) ,則每行都會得到一個如下所示的Array:

$row = [Process, MainScrenario, Process1, Step1] // first row
$row = [null, null, null, Step2] // second row

現在,我有多個Excel文件,每個文件具有不同的列數。

我試圖找到一種方法,將一個Excel的多個$ row-Arrays解析為一個具有樹結構或類似結構的大數組,以存儲信息。

有人可以幫我嗎? 我應該怎么做?

我試圖將上面的注釋組成一個編碼示例。 修改它以滿足您的需求:

<?php

// prepare data
$data = array(
  ['Process', 'MainScenario', 'Process1', 'Step1'],
  [null, null, null, 'Step2'],
  [null, null, null, 'Step3'],
  [null, null, null, 'Step3'],
  [null, null, 'Process2', 'Step1'],
  [null, null, null, 'Step2'],
  [null, 'SecScenario', 'Process1', 'Step1'],
  [null, null, null, 'Step2'],
  [null, null, 'Process2', 'Step1'],
  [null, null, null, 'Step2'],
  [null, null, null, 'Step3'],
);

// define class which holds the nodes
class node {
  private $text = '';                                      // the text (name) of the node
  private $children = array();                             // list of the children of the node

  function __construct($text) {                            // constructor. sets the text
    $this->text = $text;
  }

  function addChild(&$ref) {                               // add a child
    $this->children[] = $ref;                              // store reference to child in $this->children array property
  }

  function getText() {                                     // get text of the node
    return $this->text;                                    // retrieve textdomain
  }

  function getChildren() {                                 // get children of the node
    return $this->children;                                // fetch & deliver array
  }

  function __toString() {                                  // magic function for conversion to string
    return $this->getText();                               // is an alias for $this->getText
  }
}

// parse the data into a tree
function buildTree($data) {
  $start = array();                                         // list of all root nodes
  $last = array();                                          // list of last-seen nodes on every depth

  foreach ($data as $row) {                                 // iterate over all rows
    $depth = 0;                                             // reset depth (start form left)
    foreach ($row as $cell) {                               // iterate over all cells in the row
      if (!is_null($cell) && $cell!='') {                   // empty cell? if so, ignore
        if ($depth==0) {                                    // top layer?
          $obj = new node($cell.' (R)');                    // this is a root node
          $start[] = $obj;                                  // add to list of root nodes
          $last[0] = $obj;                                  // set as root node
        }
        else {
          $parent = null;                                   // we want to find a parent object
          for ($dd = $depth-1; $dd>=0; $dd--) {             // traverse up to root
            if (isset($last[$dd])) {                        // $last for this level set?
              $parent = $last[$dd];                         // accept it as parent
              break;                                        // do not search further
            }
          }

          if (!is_null($parent)) {                          // parent found?
            $obj = new node($cell.' ('.$depth.')');         // create new node for this entry
            $parent->addChild($obj);                        // attach it to the parent
            $last[$depth] = $obj;                           // set object as $last for this level
          }
        }
      }
      $depth++;                                             // increase depth (advance right)
    }
  }

  return $start;                                            // return list of root nodes
}

// show the tree descending from a single node given
function showTree($node, $depth = 0) {                     // recursively output nodes
  echo str_repeat('> ', $depth).$node->getText().PHP_EOL;  // this node
  foreach ($node->getChildren() as $subnode) {             // iterate over child nodes
    showTree($subnode, $depth+1);                          // recursive call for children
  }
}

// execute
$rootNodes = buildTree($data);                             // build the tree
foreach($rootNodes as $node) {                             // iterate over root nodes
  showTree($node);                                         // show tree for each of them
}

?>

生產:

Process (R)
> MainScenario (1)
> > Process1 (2)
> > > Step1 (3)
> > > Step2 (3)
> > > Step3 (3)
> > > Step3 (3)
> > Process2 (2)
> > > Step1 (3)
> > > Step2 (3)
> SecScenario (1)
> > Process1 (2)
> > > Step1 (3)
> > > Step2 (3)
> > Process2 (2)
> > > Step1 (3)
> > > Step2 (3)
> > > Step3 (3)

括號中的數字是添加節點的實際$depth級別。 (R)表示根節點。

在此處查看演示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM