doujiao7679 2015-07-03 19:08
浏览 14
已采纳

Php阵列。 环。 如果条件为真,则设置变量。 再循环。 需要重复一遍。 但不重复相同的代码

Here is longer example https://eval.in/392036

Loop through array.

If array value == to certain (first) variable[0] , then set next variable[1].

And again loop through the same array (a loop inside loop).

And again check if array value == the next variable[1] .... and again do the same.

So manually copy/paste the same code, just change variable[x].

Have such array

(
[0] => Array
    (
        [Id] => 17
        [Value] => ProductsParentId_0_CurrentId_17
        [ParentId] => 0
    )

[1] => Array
    (
        [Id] => 1
        [Value] => HomeParentId_0_CurrentId_1
        [ParentId] => 0
    )

[2] => Array
    (
        [Id] => 18
        [Value] => ComputersParentId_17_CurrentId_18
        [ParentId] => 17
    )

.....

)

And loop through

foreach ( $arr as $k_arr => $v_arr ){

if( $v_arr['ParentId'] == 0 ){
echo $v_arr['Value']. "
";
$parent_id_for_current[1] = $v_arr['Id'];

    foreach ( $arr as $k_arr => $v_arr ){
    if( $v_arr['ParentId'] == $parent_id_for_current[1] ){
    echo $v_arr['Value']. "
";
    $parent_id_for_current[2] = $v_arr['Id'];

        foreach ( $arr as $k_arr => $v_arr ){
        if( $v_arr['ParentId'] == $parent_id_for_current[2] ){
        echo $v_arr['Value']. "
";
        $parent_id_for_current[3] = $v_arr['Id'];

            .....

        }//if( $v_arr['ParentId'] == $parent_id_for_current[2] )
        }//foreach

    }//if( $v_arr['ParentId'] == $parent_id_for_current[1] )
    }//foreach

}//if( $v_arr['ParentId'] == $parent_id_for_current[0] )


}//foreach ( $arr as $k_arr => $v_arr )

Is it possible to get the same result, not repeating (copy/paste) foreach, if

Expected outcome is:

1) show ['Value'] where [ParentId] => 0

1.1) define variable with value [Id] => 17

2) loop again, find and show ['Value'] where [ParentId] == variable defined in 1.1). And repeat all. Again define another variable, then again loop, check if some [ParentId] equals to the defined variable... like in the link at top of question

  • 写回答

1条回答 默认 最新

  • dongyuntao2000 2015-07-03 19:21
    关注

    It looks like you have a recursive pattern:

    function loopMyArray(&$array, &$parent, $i)
    {
        // That's your stop condition of the recursive pattern
        // I think it's what you want.
        if (count($array) == $i)
        {
            return;
        }
        // The foreach loop call the "loopMyArray" at each iteration
        foreach ($array as $k_arr => $v_arr)
        {
            if( $v_arr['ParentId'] == $parent[$i])
            {
                echo $v_arr['Value']. "
    ";
                $parent[$i+1] = $v_arr['Id'];
                loopMyArray($array, $parent, $i+1);
            }
        }
    }
    
    $array = [...]
    $parent = [0]
    loopMyArray($array, $parent, 0);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式