# Modulus operator to get next division

• 11-26-2012, 12:47 AM
Ulysses69
Modulus operator to get next division
I have an array which starts out with 9 items. I need to split the array into equal groups, starting with 2 (the groups size will be variable though, hence why I need an equation). However, when I split the array into unequal groups, I cannot get the aesthetic effect I need and instead need to keep multiplying the array by a factor that will make equal groups.

I know how to check for multiple:
Code:

```if (\$groupsize % count(\$array) == 0) {         echo "Multiple.<br/>"; } else {         echo "Not Multiple.<br/>"; }```
But I am really getting stuck on a formula to determine how many times I need to multiply the array, for the array to be divisable by the group size.

My array contains varying text data, that will simply be appended to itself until the final array can be divided into equal groups. The groups will then be manipulated using javascript to create a seamless carousel of sorts, hence the need for equal groups.

Any help with this, as I have got no clue.
• 11-26-2012, 09:31 AM
Ulysses69
I have tried looking for no multiples and adding to the array until matched in a loop like so:

Code:

```while (\$groupsize % count(\$array) != 0) {         \$array = array_merge(\$array, \$array); }```
The above doesn't work though.
• 11-26-2012, 04:16 PM
sunfighter
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.

PHP Code:

``` <?php \$groupsize = 8;  // OR WHAT EVER NUMBER YOU WANT \$cart=array("Now","is","the","time","for","all","good","men","to"); \$test = \$cart; while(count(\$cart)%\$groupsize  != 0) {     \$cart = array_merge(\$cart, \$test); // \$TEST WILL ALWAYS BE THE SAME BEGING ARRAY     print_r(\$cart);     echo "<br />".count(\$cart)."<br /><br />"; } ?> ```
• 11-26-2012, 08:46 PM
Ulysses69
Thanks, that works as desired.

Is there however, an alternative to array_merge function, so that the appended array data is pushed to the array? as it is, the \$cart array stays the original size and not representive of the multiple additional entires that I was after, despite these entries properly populating the \$rows variable in the example.

PHP Code:

``` <?php \$groupsize = 3;  // OR WHAT EVER NUMBER YOU WANT \$cart = array("Apple","Pear","Banana","Peach","Orange","Coconut","Melon","Berries","Watermelon"); \$test = \$cart; \$count = 1; while(count(\$cart)%\$groupsize  != 0) {     //\$cart = array_merge(\$cart, \$test); // \$TEST WILL ALWAYS BE THE SAME BEGING ARRAY     \$cart = \$cart + \$test;     //print_r(\$cart);     //echo "<br />".count(\$cart)."<br /><br />";     \$count++; } foreach (\$test as \$row):     \$rows .= \$row."\n"; endforeach; echo '<h2>Original List</h2><pre>'.\$rows.'</pre>'; echo sizeof(\$test); \$i=0; foreach (\$cart as \$row):     \$i++;     \$rows .= \$row."\n";     if (\$i % \$groupsize == 0){     \$rows .= '-<br/>';     } endforeach; echo '<h2>Amended List</h2><pre>'.\$rows.'</pre>'; echo sizeof(\$cart); ?> ```
I am grateful for your help, and am just wondering if there is an alternative to array_merge to do this, without creating a new array just for this purpose.
• 11-27-2012, 04:52 AM
sunfighter
Quote:

as it is, the \$cart array stays the original size and not representive of the multiple additional entires that I was after,
Actually it does reflect the multiple additional entries. You took the heart of the while loop out of play. Why did you do this????

Put //\$cart = array_merge(\$cart, \$test); back in and then just un-comment the //print_r(\$cart); in the while loop to watch it grow. Or add a print_r(\$cart); after the loop.

This (the \$cart) stays the same only happens for a \$groupsize = 3; or 9. Try \$groupsize = 5; or 19.
• 11-27-2012, 05:35 AM
KULP
What if you found the common denominator?

And then require a whole number for the denominator.

while ( \$denominator % 1 != 0.0)

Not sure if that helps...
• 11-27-2012, 09:25 AM
Ulysses69
Quote:

Originally Posted by sunfighter
Actually it does reflect the multiple additional entries. You took the heart of the while loop out of play. Why did you do this????

Why did I do this? Because I should have known better than to do this late in the evening. Indeed, it is working:

PHP Code:

``` <?php \$groupsize = 4;  // OR WHAT EVER NUMBER YOU WANT \$cart = array("Apple","Pear","Banana","Peach","Orange","Coconut","Melon","Berries","Watermelon"); \$test = \$cart; \$count = 1; while(count(\$cart) % \$groupsize  != 0) {     \$cart = array_merge(\$cart, \$test); // \$TEST WILL ALWAYS BE THE SAME BEGING ARRAY     \$count++; } \$i=0; foreach (\$cart as \$row):     \$i++;     \$rows .= \$row."\n";     if (\$i % \$groupsize == 0){     \$rows .= '-<br/>';     } endforeach; echo '<h2>Amended List</h2><pre>'.\$rows.'</pre>'; echo sizeof(\$cart); \$rows = ''; foreach (\$test as \$row):     \$rows .= \$row."\n"; endforeach; echo '<h2>Original List</h2><pre>'.\$rows.'</pre>'; echo sizeof(\$test); ?> ```
Thanks again, and appologies for not noticing that mistake myself.
• 11-27-2012, 02:32 PM
sunfighter
Your welcome Ulysses69 and I do understand doing things on this site when your half asleep :)