我有一组使用postgres存储过程从数据库中获取的任务ID。这是我的阵列:
$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
);
有任何帮助吗? 我被困了两天。 感谢
答案 0 :(得分:1)
我认为你正在寻找这个:
$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);
答案 1 :(得分:0)
我已经尝试了解规范并且我提出了简单的解决方案
curl -w "First Byte: {time_starttransfer}, Total:{time_total}"
$ parents列出了每个级别的所有父母。因此,如果算法已经知道了不同的数字,它将添加一个项目并将其用作$ parent [$ i]
中的下一个索引(基于已存在的项目)答案 2 :(得分:0)
Online Check,这是用于测试目的的检查链接。
$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