Jump to content


Photo

Not using arrays more than once...


  • Please log in to reply
4 replies to this topic

#1 Wes1890

Wes1890
  • Members
  • PipPipPip
  • Advanced Member
  • 242 posts

Posted 16 August 2006 - 12:40 AM

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
AND THEN GOD HIMSELF BESTOWED HIS HAND UPON PHPFREAKS...

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 16 August 2006 - 01:19 AM

Very ugly but works....

#!/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);

?>


#3 Wes1890

Wes1890
  • Members
  • PipPipPip
  • Advanced Member
  • 242 posts

Posted 16 August 2006 - 01:59 AM

it returns

"Array"


thats it... ?
AND THEN GOD HIMSELF BESTOWED HIS HAND UPON PHPFREAKS...

#4 Jeremysr

Jeremysr
  • Members
  • PipPipPip
  • Advanced Member
  • 199 posts
  • LocationSaskatchewan, Canada

Posted 16 August 2006 - 02:01 AM

Maybe something like this?

<?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 />"); }
     }
}

?>


#5 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 16 August 2006 - 02:47 AM

it returns

"Array"


thats it... ?


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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users