创建动态ID并将Last循环用作父ID

时间:2018-04-26 03:17:11

标签: php loops for-loop

你好,我有一个问题,我应该如何正确地解决这个问题: enter image description here

查看第二个循环的子ID / ID的值为3.1.1

在第二个循环之后,第二个循环的子ID 必须在第三个循环中用作父ID

在每个循环中添加.1。并且父级使用他之前的最后一个循环的子ID。 预期输出:

{ID:3.1,Parent:3},
{ID:3.2,Parent:3},
{ID:3.1.1,Parent:3.1},
{ID:3.2.1,Parent:3.2},
{ID:3.1.1.1,Parent:3.1.1},
{ID:3.2.1.1,Parent:3.2.1},
{ID:3.1.1.1.1,Parent:3.1.1.1},
{ID:3.2.1.1.1,Parent:3.2.1.1}
 //and so on depends on loop count 

我的试用代码:

<?php 
$count = 6;
$a = "3.";
$b = "3";
$c = 1;
$e = "";
$f = "";
echo "<pre>";
for ($i=1; $i <=$count; $i++){
    if ($c == 1){
     $d =1;
    }
    if ($c == 2){
        $d = 2;
    }
    echo "{";
    echo "ID:".$a.$c.$e.$f.",";



    $parent = $a.$c.$e.$f;


    echo "Parent:".$parent;
    echo "}";
    if($i == $count){
    }
    else{
    echo ",";
    }

    if ($c == 2){

        $c = 0;

       $e =".1";
    }
    $f += $e;
    $c++;
}

    ?>

逻辑错误结果:

{ID:3.1,Parent:3.1},
{ID:3.20,Parent:3.20},
{ID:3.1.10.1,Parent:3.1.10.1},
{ID:3.2.10.2,Parent:3.2.10.2},
{ID:3.1.10.3,Parent:3.1.10.3},
{ID:3.2.10.4,Parent:3.2.10.4}

更多信息? click here

1 个答案:

答案 0 :(得分:2)

{ID:3.1,Parent:3},
{ID:3.2,Parent:3},
{ID:3.1.1,Parent:3.1},
{ID:3.2.1,Parent:3.2},
{ID:3.1.1.1,Parent:3.1.1},
{ID:3.2.1.1,Parent:3.2.1},
{ID:3.1.1.1.1,Parent:3.1.1.1},
{ID:3.2.1.1.1,Parent:3.2.1.1}

输出

_pluginInterface.CallStatic("ProcessImage", testTexture.GetNativeTexturePtr().ToInt32(), testTexture.width, testTexture.height);