问题:打印父子关系数组

我有一组任务 ID,我使用 postgres 存储过程从数据库中获取。这是我的数组:

  $relationships=array(
            array(70),//This represents calculated hierarchy field in a record
            array(70, 71),//This represents calculated hierarchy field in a record
            array(70, 71, 72),//This represents calculated hierarchy field in a record
            array(70, 71, 72, 68)//This represents calculated hierarchy field in a record
        );

我想以目录格式打印它们,以便能够为 MS Project 创建 XML 文件。

这个数组应该像这样打印索引:

  $relationships=array(
            array(70),//Should print 1 because its grandparent task
            array(70, 71),//Should print 1.1 because its child of task 70
            array(70, 71, 72),//Should print 1.1.1 because its child of task 71
            array(70, 71, 72, 68)//Should print 1.1.1.1
        );

有什么帮助吗?两天以来我一直被困住。谢谢

解答

在线检查,这是用于测试目的的检查链接。

$relationships=array(
            array(70),
            array(70, 71),
            array(70, 71, 72),
            array(70, 71, 72, 68),
            array(80)
        );
$old_count = 0;
$index = 0;
foreach($relationships as $val){    
    $tmp = array();
    $value = array();
    $count = count($val);
    if($count == 1){
        $old_count = 0;
        $level = array();       
        $index++;
        $level[] = $index;
    }

    if($old_count == $count)
        $level[($count-1)] = ($level[($count-1)] == "" || $level[($count-1)] == null) ? 1 : ++$level[($count-1)];
    else if($old_count > $count){
        $level[($count-1)] = ($level[($count-1)] == "" || $level[($count-1)] == null) ? 1 : ++$level[($count-1)];
        for($j = $count; $j <= $old_count; $j++)
            $level[$j] = 1;
    }
    else
        $level[$count] = 1; 

    for($i = 0; $i < $count; $i++){
        $tmp[] = $level[$i];
    }
    $old_count = $count;
    echo "Indexes: ".implode(".", $tmp)."<br/>";

结果:

Indexes: 1
Indexes: 1.1
Indexes: 1.1.1
Indexes: 1.1.2
Indexes: 1.2
Indexes: 2
Indexes: 2.1
Indexes: 2.1.1
Indexes: 2.1.2
Indexes: 2.1.2.1
Logo

PostgreSQL社区为您提供最前沿的新闻资讯和知识内容

更多推荐