Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8877371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:27:12+00:00 2026-06-14T19:27:12+00:00

I need to print an array with menu items in 4 columns, sorted vertically,

  • 0

I need to print an array with menu items in 4 columns, sorted vertically, and filled out like this:

enter image description here

Here’s the markup structure to follow (5 items).

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

I’ve beeing trying something like this, but that doesnt work because it doesnt fill out the rows first, as the above example demonstrates:

$cols = 4;
$cnt = count($items);

echo "<ul>";
foreach($items as $i => $item) {
     echo "<li>" . $item->ID . "</li>";
    if(($i + 1)%$cols == 0 && ($i + 1) != $cnt) {
        echo "</ul>\n<ul>";
    }
}
echo "</ul>";

Any help on this is appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:27:13+00:00Added an answer on June 14, 2026 at 7:27 pm

    Something like this should do the trick for what you want to do. Simply input a data array and the number of columns desired, and it will separate that array into the specified number of columns

    <?php
        $items = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17);
        $numCols = 4;
        $result = ListArrayVals($items,$numCols);
        echo $result;
        function ListArrayVals($items,$numCols) {
            $minPerRow = floor(count($items)/$numCols);
            $remaining = count($items) % $numCols;
            $colCount = array();
            for ($i = 0;$i<$numCols;$i++) {
                if ($i < $remaining) {
                    array_push($colCount,$minPerRow+1);
                } else {
                    array_push($colCount,$minPerRow);
                }
            }
            $listString = '';
            $count = 0;
            for ($i = 0;$i<count($colCount);$i++) {
                $listString = $listString . "<ul>";
                for ($j = 0;$j<$colCount[$i];$j++) {
                    $listString = $listString . '<li>' . $items[$count] . '</li>';
                    $count = $count + 1;
                }
                $listString = $listString . '</ul>';
            }
            return $listString;
        }
    ?>
    

    Just simply change the echo statements to wrap whatever HTML tags or whatever you want around it.

    EDIT I fixed it. It now will work for any number of columns and input data. I have also made it into a function so it can be re-used many times.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to print out the elements of a sorted array and also write
I need to print the below array structure as: Node Title 1 topic 1
I need to print a two-dimensional array etc: var myArray = [ [0,1,2,0], [0,1,2,0]
How to read an array list & print it in prolog ? I need
I need to print out receipts that inserted into my database, and the latest
i need to print out numbers 1-100 in a random order. the print statement
I'm currently working on a project where I need to print out a lesson
I need to sort an array of Strings in C. Here's how I'm using
I need to know the command that I can print a sentence like the
I need to sort an array of 5 numbers, this is what I have.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.