Jump to content

Not using arrays more than once...


Wes1890

Recommended Posts

Im back...

I'm needing help on a script that picks 8 people and pairs them up... but not to pair up the same pair until all people have been mixed to their max...

example

people:
a
b
c
d
e
f
g
h

(8 people with short names) (in an array)

i want it to mix up the 8 like this:

a - d
b - e
g - h
f - c

each person have 7 other people to pair up with... which makes it 64 total mix ups (i think i figured that right... lol 8*8.. it might be 7*7=49 anywyas)

so until each person is matched up with each of the other 7 people i don't want them to be paired up with someone they have been already paired up with.... get me?

any help will be awesome... thanks
Link to comment
Share on other sites

Very ugly but works....

[code=php:0]
#!/usr/bin/php
<?php

    function ugly_random_pairing($array) {

        while (count($arr) > 0) {
            $akey = array_rand($arr);
            $aval = $arr[$akey];
            unset($arr[$akey]);
            $bkey = array_rand($arr);
            $bval = $arr[$bkey];
            unset($arr[$bkey]);

            $keys[] = $aval;
            $vals[] = $bval;

        }     

        return array_combine($keys,$vals);

    }

    $arr = array('a','b','c','d','e','f','g','h');
    print_r(ugly_random_pairing($arr);

?>
[/code]
Link to comment
Share on other sites

Maybe something like this?

[code]
<?php
$people = array('a','b','c','d','e','f','g','h');

for ($x = 0; $x < 8; $x++) {
    for ($y = 0; $y < 8; $y++) {
          if ($x != $y) { echo ("$people[$x] $people[$y]<br />"); }
    }
}

?>
[/code]
Link to comment
Share on other sites

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.