如何在while循环中创建更多列?

I have this code

while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) {

        echo "
        <div><h4><a href=\"page.php?id={$row['subcategory_id']}\">{$row['subcategory']}</a></h4>
        </div>
";

    }

With this while I put on a page a vertical line with subcategories. For ex:

foo
foo
foo
foo
foo
foo
foo

I want to limit those subcategories to 10 and after each 10 to start from the top again, like this:

foo foo foo
foo foo foo
foo foo foo
foo foo foo
foo foo foo
foo foo foo
foo foo foo

Can someone tell how to do that?

Here's a working example: https://3v4l.org/MmWJA

And here's an example using your own code:

// get your array in a var
$array = mysqli_fetch_all($r, MYSQLI_ASSOC);

// set column to 10 items tall
$col_height = 10; 

// count the array
$count = count($array);

// determine number of columns based on column height and array size
$row_len = $count / $col_height;

//create iterator
$i = 0;

// loop that shits
foreach($array as $row) {

    // added display = inline 
    echo "<div style='display:inline-block;'><h4><a href=\"page.php?id={$row['subcategory_id']}\">{$row['subcategory']}</a></h4></div>
";

    // echo a line break only when needed, reset the iterator
    if($i == $row_len){
        echo "<br>";
        $i = 0;
    }
}