douhan4093 2016-07-14 19:46
浏览 49
已采纳

在数组内重复指示:重复输出该元素

I have this array (see it below) and I want to repeat each array that has a key "repeat" with a value that represents how many times to repeat.

$fields = array(
    array(
        'type'  => 'title-wrap',
        'holder' => 'h4',
        'heading'   => 'Test heading',
    ),
    array(
        'repeat' => 3,
        'type'  => 'radio',
        'name'  => 'specific_name',
        'value' => array(
            0 => 'First', // value for first repeat
            1 => 'Second', // value for second repeat
            2 => 'Third' // value for third repeat
        ),
    )
);

For that I have created a recursive function named generateForm:

function generateForm($fields, $index = 0) {
    if ( $fields == '' ) { return false; }
    foreach ($fields as $field) {
        if ( isset($field['type']) ) {
            switch ( $field['type'] ) {
            case 'title-wrap':
                echo $field['heading'];
                break;
            case 'radio':
                echo $field['value'][$index];
                break;
            }
        }
        if ( isset($field['repeat']) ) {
            for ($i=0; $i < $field['repeat']-1; $i++) { 
                generateForm($field, $i);
            }
        }
    }
}

The output I want:

Test heading
First
Second
Third

But I don't get the last two words in the output. What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dongyuan9292 2016-07-14 20:14
    关注

    If you want to stick with the recursive method, then you need to correct some issues:

    • The recursive call you have does not pass an array, but an element of an array. Yet the function expects an array. So you should wrap it in an array construct when passing the first argument: [$field].
    • Once the above is fixed, the test on the repeat key will succeed again when you are in a recursive call, and so you will start that loop again, and get into an endless recursion. To prevent this from happening, don't loop, but just call the function recursively for the next repetition if there is at least one more to go.

    Here is the corrected code:

    function generateForm($fields, $index = 0) {
        if ( $fields == '' ) { return false; }
        foreach ($fields as $field) {
            if ( isset($field['type']) ) {
                switch ( $field['type'] ) {
                case 'title-wrap':
                    echo $field['heading'] . "
    ";
                    break;
                case 'radio':
                    echo $field['value'][$index] . "
    ";
                    break;
                }
            }
            if ( isset($field['repeat']) && $index < $field['repeat'] - 1) {
                generateForm([$field], $index + 1);
            }
        }   
    }   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题