duanqi5333 2017-04-07 00:39
浏览 68
已采纳

如何从数组格式化族谱

I'm trying to create a pedigree and I've got the function to both build and to display the data, I'm just not sure how to format it correctly on output.

I'm trying to work with this css/html http://jsfiddle.net/bZGFA/32/

My array output of the tree information looks like this:

Array(
 [id] => IJ6wF
 [mother] => Array (
     [id] => BzOqN
     [mother] => Array (
         [id] => 1G3Pl
         [mother] => 
         [father] =>
     )
     [father] => Array (
         [id] => xWDlH
         [mother] => 
         [father] =>
     )
 )
 [father] => Array (
     [id] => Rstov
     [mother] =>
     [father] =>
 )
)

I'm using a recursive function to build the array. I'm using the following function I found on stackoverflow to display the array.

function displayArrayRecursively($arr, $indent='') {
    if ($arr) {
        foreach ($arr as $value) {
            if (is_array($value)) {
                //
                displayArrayRecursively($value, $indent . '--');
            } else {
                //  Output
                if (!empty($value)) {
                    echo "$level $indent $value 
";
                }
            }
        }
    }
}

I'm lost on how I can configure the function to build the table. Any help would be amazing. Thanks!

  • 写回答

1条回答 默认 最新

  • doupi4649 2017-04-07 03:25
    关注
    printTree($array) {
        $out = "<li>" . "<a href='#'>". $array["id"] . "</a>";
    
        if (array_key_exists("mother", $array) || array_key_exists("father", $array)) {
    
            $out .= "<ul>";
    
            if (array_key_exists("mother", $array)) {
                $out .= printTree($array["mother"]);
            }
    
            if (array_key_exists("father", $array)) {
                $out .= printTree($array["father"]);
            }
    
            $out .= "</ul>";
        }
    
        $out .= "</li>";
    
        return $out;
    }
    

    While in the html use:

    <ul class="tree desc3">
        <?php echo printTree($array); ?>
    </ul>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用