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

如何将这个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条)

报告相同问题?