Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php上为分支根创建逻辑重复_Php_Repeat - Fatal编程技术网

如何在php上为分支根创建逻辑重复

如何在php上为分支根创建逻辑重复,php,repeat,Php,Repeat,我想为repeat创建一个逻辑重复,并在php上做一个分支,结果如下(这是没有重复的逻辑,只是html): 请注意,如果您的数据集很大,或者代码变得比每个列表元素中的简单文本字符串更复杂,您可能希望缓存递归函数的结果,并且仅在数据更新时重建树 如果我知道可以通过其他函数访问每个家长和孩子,我会传入当前的家长id。递归在工作时总是很棘手且看似简单 // assuming getChildIds( $parentId ) is a functi

我想为repeat创建一个逻辑重复,并在php上做一个分支,结果如下(这是没有重复的逻辑,只是html):


请注意,如果您的数据集很大,或者代码变得比每个列表元素中的简单文本字符串更复杂,您可能希望缓存递归函数的结果,并且仅在数据更新时重建树

如果我知道可以通过其他函数访问每个家长和孩子,我会传入当前的家长id。递归在工作时总是很棘手且看似简单

// assuming getChildIds( $parentId ) is a function that returns an array of ids
// assuming getName( $id ) is a function that returns a name. 
// If you actually want "Parent", "Child", ... "Grand Grand ... Grand Child" 
// add a generation parameter to the recursive function and figure it out that way.

function echo_child_node( $parentId ) { ?>
    <li>
        <a href="#"><?php echo getName( $parentId ) ?></a>
        <?php $childIds = getChildIds( $parentId ); ?>
        <?php if ( $childIds ) : ?>
            <ul>
            <?php foreach( $childIds as $childId ) :
                echo_child_node( $childId );
            } ?>
            </ul>
        <?php endif; ?>
   </li>
<? }
//假设getChildId($parentId)是一个返回ID数组的函数
//假设getName($id)是一个返回名称的函数。
//如果你真的想要“父母”、“孩子”。。。“孙子,孙子,孙子”
//向递归函数中添加一个生成参数,并以这种方式进行计算。
函数echo\u child\u节点($parentId){?>

  • 请注意,如果您的数据集很大,或者代码变得比每个列表元素中的简单文本字符串更复杂,那么您可能希望缓存递归函数的结果,并且仅在数据更新时重建树

    如果我知道可以通过其他函数访问每一个父项和子项,我会将当前的父项id传递进来

    // assuming getChildIds( $parentId ) is a function that returns an array of ids
    // assuming getName( $id ) is a function that returns a name. 
    // If you actually want "Parent", "Child", ... "Grand Grand ... Grand Child" 
    // add a generation parameter to the recursive function and figure it out that way.
    
    function echo_child_node( $parentId ) { ?>
        <li>
            <a href="#"><?php echo getName( $parentId ) ?></a>
            <?php $childIds = getChildIds( $parentId ); ?>
            <?php if ( $childIds ) : ?>
                <ul>
                <?php foreach( $childIds as $childId ) :
                    echo_child_node( $childId );
                } ?>
                </ul>
            <?php endif; ?>
       </li>
    <? }
    
    //假设getChildId($parentId)是一个返回ID数组的函数
    //假设getName($id)是一个返回名称的函数。
    //如果你真的想要“父母”,“孩子”,“孙子,孙子,孙子”
    //向递归函数中添加一个生成参数,并以这种方式进行计算。
    函数echo\u child\u节点($parentId){?>
    

  • 这是通过使用一个递归函数和一个嵌套数组来完成的,然后遍历该数组

    <?php
    function makeNav($item) {
        $ret = '<li><a href="'.$item['url'].'">'.$item['name'].'</a>'.PHP_EOL;
    
        if (isset($item['subPgs']) && is_array($item['subPgs']) && count($item['subPgs']) > 0) {
            $ret .= '<ul>'.PHP_EOL;
            foreach ($item['subPgs'] as $subPg) {
                $ret .= makeNav($subPg);
            }
            $ret .= '</ul>'.PHP_EOL;
        } else {
            $ret .= '</li>'.PHP_EOL;
        }
    
        if (isset($item['subPgs']) && is_array($item['subPgs']) && count($item['subPgs']) > 0) {
            $ret .= "</li>".PHP_EOL;
        }
    
        return $ret;
    }
    
    $navItems = array(
        /*array(
            'name' => 'Home',
            'url' => '#',
            'subPgs'=>array()
        ),*/
        array(
            'name' => 'Parent',
            'url' => '#',
            'subPgs' => array(
                array(
                    'name' => 'Child',
                    'url' => '#',
                    'subPgs' => array(
                        array(
                            'name' => 'Grand Child',
                            'url' => '#'
                        ),
                        array(
                            'name' => 'Grand Child',
                            'url' => '#',
                            'subPgs' => array(
                                array(
                                    'name' => 'Grand Child Child',
                                    'url' => '#'
                                ),
                                array(
                                    'name' => 'Grand Child Child',
                                    'url' => '#'
                                )
                            )
                        )
                    )
                ),
                array(
                    'name' => 'Child',
                    'url' => '#',
                    'subPgs' => array(
                        array(
                            'name' => 'Grand Child',
                            'url' => '#',
                            'subPgs' => array(
                                array(
                                    'name' => 'Grand Grand Child',
                                    'url' => '#'
                                )
                            )
                        ),
                        array(
                            'name' => 'Grand Child',
                            'url' => '#'
                        )
                    )
                )
            )
        )
    );
    
    $nav = '<ul>';
    foreach ($navItems as $navItem) {
        $nav .= makeNav($navItem);
    }
    echo $nav.'</ul>';
    ?>
    
    
    
    输出:(与OPs匹配,格式除外)


    联机查看:

    这是通过使用递归函数和嵌套数组来完成的,然后遍历该数组。以下函数可以深入n个级别而不会出现问题

    <?php
    function makeNav($item) {
        $ret = '<li><a href="'.$item['url'].'">'.$item['name'].'</a>'.PHP_EOL;
    
        if (isset($item['subPgs']) && is_array($item['subPgs']) && count($item['subPgs']) > 0) {
            $ret .= '<ul>'.PHP_EOL;
            foreach ($item['subPgs'] as $subPg) {
                $ret .= makeNav($subPg);
            }
            $ret .= '</ul>'.PHP_EOL;
        } else {
            $ret .= '</li>'.PHP_EOL;
        }
    
        if (isset($item['subPgs']) && is_array($item['subPgs']) && count($item['subPgs']) > 0) {
            $ret .= "</li>".PHP_EOL;
        }
    
        return $ret;
    }
    
    $navItems = array(
        /*array(
            'name' => 'Home',
            'url' => '#',
            'subPgs'=>array()
        ),*/
        array(
            'name' => 'Parent',
            'url' => '#',
            'subPgs' => array(
                array(
                    'name' => 'Child',
                    'url' => '#',
                    'subPgs' => array(
                        array(
                            'name' => 'Grand Child',
                            'url' => '#'
                        ),
                        array(
                            'name' => 'Grand Child',
                            'url' => '#',
                            'subPgs' => array(
                                array(
                                    'name' => 'Grand Child Child',
                                    'url' => '#'
                                ),
                                array(
                                    'name' => 'Grand Child Child',
                                    'url' => '#'
                                )
                            )
                        )
                    )
                ),
                array(
                    'name' => 'Child',
                    'url' => '#',
                    'subPgs' => array(
                        array(
                            'name' => 'Grand Child',
                            'url' => '#',
                            'subPgs' => array(
                                array(
                                    'name' => 'Grand Grand Child',
                                    'url' => '#'
                                )
                            )
                        ),
                        array(
                            'name' => 'Grand Child',
                            'url' => '#'
                        )
                    )
                )
            )
        )
    );
    
    $nav = '<ul>';
    foreach ($navItems as $navItem) {
        $nav .= makeNav($navItem);
    }
    echo $nav.'</ul>';
    ?>
    
    
    
    输出:(与OPs匹配,格式除外)


    在线查看:

    你的意思是“递归”@ArtisticPhoenix如果使用递归创建,如何创建2个分支,每个分支都有一个导数?我觉得必须使用Repetitio创建我通常不会在假日周末执行递归。要执行重复,你需要事先知道最大级别数,递归是<无限的。假设你有一个函数<代码>buildItem()
    当你击中一个孩子时,你从
    buildItem()
    中调用
    buildItem()
    ,并构建该子项-tree@ArtisticPhoenix你能给我举个例子吗?你是说“递归”@ArtisticPhoenix如果你用递归创建,如何创建2个分支,每个分支都有一个导数?我觉得它必须用重复创建。我通常不会在节假日做递归