Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing array in parent-child relationship

I have an array of task ids that I fetch from database using postgres stored procedure. 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.

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


1 Answers

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);
like image 167
Kawsar Ahmed Avatar answered Jan 31 '26 12:01

Kawsar Ahmed