Jump to content

array_push and array_chunk difficulties


Go to solution Solved by Barand,

Recommended Posts

Our disc golf league uses a little PHP script that I developed (stealing lots of code in the process) that sucks in a list of league players from a text file, lets you select the ones that show up on league day, and randomly assigns the selections into foursomes and threesomes (Demo: http://edge.byethost18.com/demo4/foursomes.php)

Here's a modified example using letters of the alphabet:
https://neth.roe3.org/mdrone/ABCs/foursomes.php

. . . and the code:
https://neth.roe3.org/mdrone/ABCs/foursomes.txt

The actual version uses the shuffle() function to randomize the assignments and has a refresh button to reshuffle the assignments if a grouping contains bitter enemies. I disabled it in the example in order to show how the "A-B-C"s are assigned to the groups. As you can see, it assigns "A" to the first group, "B" to the second group, "C" to the third group and so on until it reaches the last group and then starts back at group 1 with the next letter.

One of the our players wants a modification. He wants to be able to assign players into foursomes/threesomes according to their handicaps (best to worst). In other words, Group 1 would contain A, B, C, D. Group 2 would contain E, F, G, H and so on. Naturally, this would depend on which player checkboxes are selected, but the order is maintained.

What I am having trouble with is getting the "A-B-C"s assigned into groups in the order they are listed in the text file.

I tried using array_chunk() in the following script:

https://neth.roe3.org/mdrone/ABCs/foursomes2.php

. . . and the code:
https://neth.roe3.org/mdrone/ABCs/foursomes2.txt

In a nutshell, here's the code that's doing the work . . .

$numgroups = $d;
if($howmany % 4 == 0) {
 $numgroups = '4';

$rows = array_chunk($players,$numgroups); 

$x=0;
foreach ($rows as $row) {
$cnt=$x+1;
    echo "<b><font color='#F44336'>Group ".$cnt."</font></b><br/>";
    echo "<ul>";
      foreach ($row as $value) {
         print "<li>" . $value . "\n";
        }
    echo "</ul>";
  $x++;
 }
 
Unfortunately, I have a mental block when it comes to getting it to create groups like the original (which I prefer). For instance, if you select all items in the original script, Groups 6 & 7 will contain 3 items each.  When you select all items in the array_chunk() version, Groups 6 & 7 contain 4 items and 2 items respectively.  The same occurs if you select 10 items: the original script groups |4|3|3| and the array_chunk() version groups |4|4|2|. 

I'm sure there's a simple solution. I'm just not seeing it. I'm really just a hobbyist . . . not a real programmer by any stretch of the imagination.

Thanks for any help anyone can send my way. BTW . . . if anyone is interested in the foursomes generator source, I'm happy to share it. It could probably use improvement. 

Link to comment
https://forums.phpfreaks.com/topic/312074-array_push-and-array_chunk-difficulties/
Share on other sites

  • Solution

Are you wanting something like this?

function golfGroups($players)
{
    $n = count($players);
    if ($n < 6) return $players;
    
    $num_3s = $n%4 ? 4 - $n%4 : 0;
    $num_remain = $n - $num_3s*3;
    
    $fours = array_chunk(array_slice($players, 0, $num_remain), 4);
    $threes = $num_3s ? array_chunk(array_slice($players, -$num_3s*3), 3) : [];
    return array_merge($fours, $threes);
}

##
##  TEST IT
##
for ($i=6; $i<=25; $i++) {
    $p = range(1,$i);
    $p = array_map( function($v) { return "Player $v";}, $p);   // create array of players
    
    echo "<h3>$i players</h3>";
    echo '<pre>', print_r(golfGroups($p), 1), '</pre>';
    echo '<hr>';
}

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.