简体   繁体   中英

How to range and sequence in PHP

i need a sequence like this in a range from 0 to 10 with PHP. The result would be:

0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10 ........

I want to insert this function inside a foreach

UPDATE

<?php $count = 0; ?>
<div class="<?php echo 'minipost-'. $count++ ; ?>">
<?php
get_template_part( 'content', get_post_format() );
?>
</div>

You already mentioned the solution in your question. You can use:

range(0, 10)

To repeat the sequence 3 times you could do:

str_repeat(implode(' ',range(0, 10)).' ', 3);

Without foreach;

$iCounter = 2;
$iCount = 10;

for ($k = 1; $k <= $iCounter; $k++) {
    for ($i = 0; $i <= $iCount; $i++) {
        // do anything you need
        echo $i;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM