How to create a php loop to print in specific pattern?

aslamdoctor

I want to create a PHP Loop so that it should read an array of names and print it in a specific pattern so that on every 4 records, it should close and reopen the new UL element. So in general it should be like below :

$names = array('Name 1', 'Name 2', 'Name 3', 'Name 4', 'Name 5', 'Name 6', 'Name 7', 'Name 8', 'Name 9');

This is the array I have and I want to create a loop so it should print like this

<ul>
    <li>Name 1</li>
    <li>Name 2</li>
    <li>Name 3</li>
    <li>Name 4</li>
</ul>

<ul>
    <li>Name 5</li>
    <li>Name 6</li>
    <li>Name 7</li>
    <li>Name 8</li>
</ul>

<ul>
    <li>Name 9</li>
</ul>
waka-waka-waka
$count = 0;
foreach ($names as $key => $value) {
    if ($count%4 == 0) {
        print '<ul>';
    }

    print "<li>$value</li>";

    if ($count%4 == 3) {
        print '</ul>';
    }
    $count++;
}
if (count($names)%4 != 0) {
    print '</ul>';
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I create a loop to print the following pattern?

How to print specific pattern with sed

How to create table using loop and print variables in it | using PHP

How to print this pattern using PHP?

How to print '*' pattern printing using JavaScript loop?

How to dynamically create a new array based on a specific value with a loop PHP

How to add a specific pattern to my loop?

How to print pattern with an increasing of multiples of 10 in php?

How i do a print for a specific variable in a loop?

How to create a regex pattern for splitting at specific chars?

How to create a regex pattern for a specific pattern in json file in scala

How create a foreach loop to print array in reverse?

For loop to ouput a specific pattern

How To Check Specific URL Pattern Via PHP?

How to pull data that are in a specific pattern in MYSQL and PHP

How can I print an int[] of numbers to a specific pattern?

How to print a specific row without using pattern matching

How to print first column of row along with specific pattern?

How to iterate the loop for a matched pattern and print the count using awk

how to print specific content in json with php

How print specific duplicate value of php array

How to create this loop (WordPress/PHP)

Print letters in specific pattern in Python

print multiple words in specific pattern

Java print a pattern with nested for loop

To print a pattern in Python using 'for' loop

PYTHON - How to create a loop to prompt a specific answer

How to create variable with if multiple statement in php and print

How to make a for loop read files using a specific pattern