PHP循环插入

Consider the following loop:

            <?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
            <?php if($extraField->value != ''): ?>
                <div class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
                    <span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
                </div>
            <?php endif; ?>
        <?php endforeach; ?>

I wanted to wrap the first 12 items in a div and then the last 2 items in a div. The problem is there are not always 12 items exactly in the first div. There can be between 2 AND 12 items.

How would I manipulate this loop to achieve such? Many thanks

Just use a counter inside the loop to see how many times you've been through it.

<?php $count = 1; ?>
<?php $break= count($this->item->extra_fields) - 2; ?>
<?php echo "<div>"; ?>
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
    <?php if($extraField->value != ''): ?>
        <div class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
            <span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
        </div>
        <?php $count++; ?>
    <?php endif; ?>
    <?php if ($count == $break) : ?>
        <?php echo "</div><div>"; $count ==0; ?>
    <?php endif; ?>
<?php endforeach; ?>
<?php echo '</div>'; ?>