du5739 2019-06-07 19:22
浏览 395
已采纳

如何将这个foreach循环限制为10个循环?

How can I limit the code below to show 10 loops.

        foreach( $entries as $entry ) {
            echo '<tr>';
            $fields = wpforms_decode( $entry->fields );
            foreach( $fields as $field ) {
                if ( in_array( $field['id'], $ids)) {
                    echo '<td>' . apply_filters( 'wpforms_html_field_value', wp_strip_all_tags( $field['value'] ), $field, $form_data, 'entry-frontend-table' );
                }
            }
            echo '</tr>';
        }
  • 写回答

2条回答 默认 最新

  • douzhong3887 2019-06-07 20:02
    关注

    One possibility where the loop to break is the first one:

    foreach( $entries as $key=>$entry ) 
    {
        if($key==9) break;
        echo '<tr>';
        $fields = wpforms_decode( $entry->fields );
    
        foreach( $fields as $field ) 
        {
            if ( in_array( $field['id'], $ids)) 
            {
                echo '<td>' . apply_filters( 'wpforms_html_field_value', wp_strip_all_tags( $field['value'] ), $field, $form_data, 'entry-frontend-table' );
            }
        }
        echo '</tr>';
    }
    

    Another possiblity where the loop to break is the second one:

    foreach( $entries as $entry ) 
    {
        echo '<tr>';
        $fields = wpforms_decode( $entry->fields );
        foreach( $fields as $key=>$field ) 
        {
            if($key==9) break;
            if ( in_array( $field['id'], $ids)) 
            {
                echo '<td>' . apply_filters( 'wpforms_html_field_value', wp_strip_all_tags( $field['value'] ), $field, $form_data, 'entry-frontend-table' );
            }
        }
        echo '</tr>';
    }
    

    If you want to stop the whole process / method / function you might want to use return instead of break. Break will just stop the current loop process.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型