如何限制下面的代码显示10个循环。
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>';
}
答案 0 :(得分:0)
像这样:
$i = 0;
foreach( $entries as $entry ) {
$i++;
if ($i > 9 ) break; // this will stop after the 10th loop and in the beginning of loop 11
echo '<tr>';
$fields = wpforms_decode( $entry->fields );
foreach( $fields as $field ) {
// if you want to stop this loop too use $ii not $i
// but notice stopping this loop will not stop the parent loop !
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>';
}
答案 1 :(得分:-1)
第一个可能发生循环中断的可能性:
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>';
}
另一个可能发生的循环中断是第二个循环:
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>';
}
如果要停止整个过程/方法/功能,则可能要使用return而不是break。中断只会停止当前的循环过程。