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
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. 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]
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.