Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    May 2004
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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.
    Last edited by Ulysses69; 11-25-2012 at 11:55 PM.

  • #2
    New Coder
    Join Date
    May 2004
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.
    Last edited by Ulysses69; 11-26-2012 at 09:19 AM.

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,085
    Thanks
    23
    Thanked 593 Times in 592 Posts
    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 />";
    }
    ?>

  • #4
    New Coder
    Join Date
    May 2004
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.
    Last edited by Ulysses69; 11-26-2012 at 07:59 PM.

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,085
    Thanks
    23
    Thanked 593 Times in 592 Posts
    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.
    Last edited by sunfighter; 11-27-2012 at 03:55 AM.

  • Users who have thanked sunfighter for this post:

    Ulysses69 (11-27-2012)

  • #6
    Regular Coder
    Join Date
    Mar 2012
    Posts
    168
    Thanks
    5
    Thanked 11 Times in 11 Posts
    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...

  • #7
    New Coder
    Join Date
    May 2004
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sunfighter View Post
    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.

  • #8
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,085
    Thanks
    23
    Thanked 593 Times in 592 Posts
    Your welcome Ulysses69 and I do understand doing things on this site when your half asleep


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •