简体   繁体   English

以父子关系打印数组

[英]Printing array in parent-child relationship

I have an array of task ids that I fetch from database using postgres stored procedure. 我有一个使用postgres存储过程从数据库中获取的任务ID数组。 Here is my array: 这是我的数组:

  $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
        );

I want to print them in table of contents format to be able to create XML file for MS Project. 我想在目录格式中打印它们,以便能够为MS Project创建XML文件。

This array should print indexes like this: 这个数组应该打印这样的索引:

  $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
        );

Any help? 有帮助吗? I have been stuck since two days. 我被困了两天。 Thanks 谢谢

I think you are looking for this: 我想你正在寻找这个:

$relationships = array(
    array(70, 71, 72),      // 1.1.1
    array(80),              // 2
    array(75, 71, 72),      // 3.1.1
    array(80, 72),          // 2.1
    array(75, 72, 72),      // 3.2.1
    array(70),              // 1
    array(70, 71, 74),      // 1.1.2
    array(80, 71, 1, 2, 3, 4, 5, 6, 7, 8, 9),           // 2.2.1.1.1.1.1.1.1.1.1
    array(80, 71, 1, 2, 3, 4, 5, 6, 7, 8, 0)            // 2.2.1.1.1.1.1.1.1.1.2
);

function find_all($values, &$arr){
    if(count($values) == 0){
        echo "\n"; return;
    }
    if(array_key_exists($values[0], $arr))
        echo (array_search($values[0], array_keys($arr))+1).'.';
    else {
        echo (count($arr)+1).'.';
        $arr[$values[0]] = array();
    }
    find_all(array_slice($values, 1), $arr[$values[0]]);
}

$storage = array();
foreach($relationships as $array)
    find_all($array, $storage);

I've tried to understand the spec and I've come up with simple solution 我试图理解规范,我想出了简单的解决方案

$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
    array(70, 71, 72, 69),//Should print 1.1.1.2
    array(70, 73), //Should print 1.2

);

$parents = array();

foreach($relationships as $row) {
    // parsing row
    $row_output = array();
    for($i = 0; $i < count($row); $i++)
    {

        if(!isset($parents[$i]))
            $parents[$i] = array();

        $index = array_search($row[$i], $parents[$i]);
        if($index !== FALSE) {
            $row_output[] = $index+1;
        }
        else {
            $index = count($parents[$i]);
            $parents[$i][] = $row[$i];
            $row_output[] = $index+1;
        }
    }
    echo implode('.', $row_output) . "\n";
}

// var_export($parents);

the $parents is listing all the parents on each level. $ parents列出了每个级别的所有父母。 So if there's a different number that algorithm already know, it will add an item and use it as next index (based on already present items) in $parent[$i] 因此,如果算法已经知道不同的数字,它将添加一个项目并将其用作$ parent [$ i]中的下一个索引(基于已存在的项目)

Online Check , This is the checking link for testing purpose. 在线检查 ,这是用于测试目的的检查链接。

$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/>";

Result: 结果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM