Jump to content

Archived

This topic is now archived and is closed to further replies.

Wes1890

Not using arrays more than once...

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

Share this post


Link to post
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]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
[quote]
it returns

"Array"


thats it... ?
[/quote]

Not here it does'nt. Show me how your using it.

Share this post


Link to post
Share on other sites

×

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.