【问题标题】:Recursive Tree formatted to specific HTML格式化为特定 HTML 的递归树
【发布时间】:2021-04-19 14:55:40
【问题描述】:

有人可以帮我用正确的逻辑将分层数组格式化为以下 HTML 模式吗?我意识到这对于许多帖子来说是半冗余的,但我似乎无法深入了解并退出递归过程以格式化我需要的方式。

这个 PHP 样式数组是使用从数据库提供的递归函数创建的。就创建数组而言,递归函数运行良好。这是一个示例数组结构,其中包含在 HTML 中格式化后的相应图片。请注意,“c”子节点是一对多子节点的数组,可能具有任意数量的附加“c”节点。每个叶节点正确地终止给定链,因此在这种情况下,'DELIVERY POINT' 和 'PORT' 是具有直接父级的叶节点,如图所示。

硬编码格式化分层结果示例:

array (
  0 => 
  array (
    'PKEY_MODULE' => '4',
    'PKEY_ENTITY' => '15',
    'ENTITY' => 'LEGAL ENTITY',
    'PKEY_ENTITY_PARENT' => '0',
    'c' => 
    array (
      0 => 
      array (
        'PKEY_MODULE' => '6',
        'PKEY_ENTITY' => '29',
        'ENTITY' => 'PHYSICAL SITE',
        'PKEY_ENTITY_PARENT' => '15',
        'c' => 
        array (
          0 => 
          array (
            'PKEY_MODULE' => '6',
            'PKEY_ENTITY' => '30',
            'ENTITY' => 'ADDRESS',
            'PKEY_ENTITY_PARENT' => '29',
            'c' => 
            array (
              0 => 
              array (
                'PKEY_MODULE' => '6',
                'PKEY_ENTITY' => '31',
                'ENTITY' => 'DELIVERY POINT',
                'PKEY_ENTITY_PARENT' => '30',
              ),
            ),
          ),
          1 => 
          array (
            'PKEY_MODULE' => '6',
            'PKEY_ENTITY' => '32',
            'ENTITY' => 'STRUCTURE',
            'PKEY_ENTITY_PARENT' => '29',
            'c' => 
            array (
              0 => 
              array (
                'PKEY_MODULE' => '6',
                'PKEY_ENTITY' => '33',
                'ENTITY' => 'AREA',
                'PKEY_ENTITY_PARENT' => '32',
                'c' => 
                array (
                  0 => 
                  array (
                    'PKEY_MODULE' => '6',
                    'PKEY_ENTITY' => '34',
                    'ENTITY' => 'LOCATION',
                    'PKEY_ENTITY_PARENT' => '33',
                    'c' => 
                    array (
                      0 => 
                      array (
                        'PKEY_MODULE' => '6',
                        'PKEY_ENTITY' => '35',
                        'ENTITY' => 'PORT',
                        'PKEY_ENTITY_PARENT' => '34',
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    ),
  ),
)

这就是 HTML 最终的样子 - 我硬编码了这个示例,以便与链接图像中显示的进行比较。

    <div class="hv-item">
       <div class="hv-item-parent"><p class="node">LEGAL ENTITY</p></div>
       <div class="hv-item-children">
           <div class="hv-item-child">
             <div class="hv-item">
                <div class="hv-item-parent"><p class="node">PHYSICAL SITE</p></div>
                <div class="hv-item-children">
                   <div class="hv-item-child">
                      <div class="hv-item">
                         <div class="hv-item-parent"><p class="node">ADDRESS</p></div>
                         <div class="hv-item-children">
                            <div class="hv-item-child">
                               <p class="node">DELIVERY POINT</p>
                            </div>
                         </div>
                      </div>
                   </div>
                   <div class="hv-item-child">
                      <div class="hv-item">
                         <div class="hv-item-parent"><p class="node">STRUCTURE</p></div>
                         <div class="hv-item-children">
                             <div class="hv-item-child">
                               <div class="hv-item">
                                  <div class="hv-item-parent"><p class="node">AREA</p></div>
                                  <div class="hv-item-children">
                                     <div class="hv-item-child">
                                        <div class="hv-item">
                                           <div class="hv-item-parent"><p class="node">LOCATION</p></div>
                                           <div class="hv-item-children">
                                              <div class="hv-item-child"><p class="node">PORT</p></div>
                                           </div>
                                        </div>
                                     </div>
                                  </div>
                               </div>
                            </div>
                         </div>
                      </div>
                   </div>
                </div>
             </div>
          </div>
       </div>
    </div>

作为参考,这里是我用来格式化数组的递归函数 - 我从堆栈中取出它。

function get_entity_hierarchy($array, $parent) {
   $branch = array();
   foreach ($array as $a) {
      if ($a["PKEY_ENTITY_PARENT"] == $parent) {        
         $child = get_entity_hierarchy($array, $a["PKEY_ENTITY"]);
         if ($child) {
            $a['c'] = $child;
         }
         $branch[] = $a;         
      }
   }
   return $branch;
}

最后但并非最不重要的是,这是我开始使用的数据集 - 作为数据库中的关联数组返回:

数据行:

【问题讨论】:

    标签: php html hierarchy recursive-datastructures


    【解决方案1】:

    我还没有修复几个尾随 div 节点,对于具有多个父节点(本质上是平行树)的模块,格式化需要一些工作,但这让我现在已经足够接近马蹄铁了。 $str 和 $recursive_count 变量是静态的,并在函数调用之前声明。

    function format_tree($array) {
       $str = '';
       static $recursive_count = 0;
       foreach ($array as $key => $value) {
          if (is_array($value)) {
             if ($key > 0 && $value["PKEY_ENTITY_PARENT"] != 0) { $str .= '<div class="hv-item-child">'; }
             if ($key > 0 && $value["PKEY_ENTITY_PARENT"] == 0) { $str .= '<br><br>'; }
             if (array_key_exists('c', $value)) {
                $str .= '<div class="hv-item">';
                $str .= '<div class="hv-item-parent"><p class="node">'.html_request_link('M','R03', $value["PKEY_ENTITY"], $value["ENTITY"]).'</p></div>';
                $str .= '<div class="hv-item-children">';
                $str .= '<div class="hv-item-child">';
                $recursive_count++;
                $str .= format_tree($value['c']);
                $recursive_count--;
                if ($key > 0 && $value["PKEY_ENTITY_PARENT"] != 0) {$str .= '</div>';}
                if ($recursive_count > 0) $str .= '</div></div></div>';
             } else {
                $str .= '<p class="node">'.html_request_link('M','R03', $value["PKEY_ENTITY"], $value["ENTITY"]).'</p></div>';
             }
          }
       }
       return $str;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 2014-11-16
      • 2011-11-06
      • 1970-01-01
      相关资源
      最近更新 更多