Ulysses69 it is hard to understand exactly what you mean when you say $groupsize. Is this the number of groups or the amount of things in a group?
It doesn't matter for the solution though.
Let's start with your first misconception: $groupsize % count($array) == 0 Not correct you want count($array) % $groupsize == 0.
Your $array = array_merge($array, $array); will not work as you think because your $array will be twice as big each time this is executed.
For the answer to you question. You don't need a formula, you need a while loop. I add a print_r and an echo so you can see the progression.
$groupsize = 8; // OR WHAT EVER NUMBER YOU WANT
$test = $cart;
while(count($cart)%$groupsize != 0)
$cart = array_merge($cart, $test); // $TEST WILL ALWAYS BE THE SAME BEGING ARRAY
echo "<br />".count($cart)."<br /><br />";