Imagine a slot machine with 3 slots and where each slot has 3 things that could come up; A, B, or C. Each item (A,B, or C) can only come up once. (This is just for this example although sometimes it feels like that in Vegas)
Is there a way to list all of the possible combinations?

A - B - C
A - C - B
B - C - A
B - A - C
C - B - A
C - A - B

    how do you store the differnet values for each "slot"? in an array?

      I can do it any way that will work efficiently...An example with an array would be great.

      I can think of a bunch of ways to do it that are rickety, non-scaleable, and waaaay inefficient...but there has got to be a simple way that I'm just too Monday to see right now.

        after a few cup of coffee and some server errors i found a solution. it's not yet the 'best' but a start to think of how to increase the performance.

        Add more values to the array just as you like to.
        Attention: i didn't test it with "fixex key-value"-pairs that need to be associated! (i mean e.g. [a]=3, [d]=5 .... and the sort it)

        must admit that i'm a little bit proud of that code 😉

        $start_array = array("A", "B", "C");
        function go($string, $del_array)
        {
          global $start_array;
        
          if($del_array)
            $new_array = array_diff($start_array,$del_array);
          else
            $new_array = $start_array;
        
          if(sizeof($new_array)>0)
          {
            while(list($key,$val)=each($new_array))
            {
              $del_array[$key] = $val;
              go($string." ".$val,$del_array);
              $temp[] = $val;
              $del_array = array_diff($del_array,$temp);
            }
          }
          else echo "<b>".$string."</b><br>";
        }
        go("",false);
        

          Just run through all combos and discard the one you don't want.

          $slot1 = array('A','B','C');
          $slot2 = array('A','B','C');
          $slot3 = array('A','B','C');
          
          foreach($slot1 as $r1)
          {
            foreach($slot2 as $r2)
            {
              foreach($slot3 as $r3)
              {
                filter($r1,$r2,$r3);
              }
            }
          }
          function filter($r1,$r2,$r3)
          {
            if ($r1 == $r2) return;
            if ($r1 == $r3) return;
            if ($r2 == $r3) return;
            echo "$r1 - $r2 - $r3<br />\n";
          }
          

            What you want is generally called a "Monte Carlo" algorithm (your "Vegas" comment was closer than you thought). I doubt this link will be much use but it is kind of entertaining. Do a little more searching for Monte Carlo algorithms than I did...

            http://psroc.phys.ntu.edu.tw/cjp/v38/605.pdf

              You guys rock like limestone...volleytotal.ch and ahundiak, your suggestions both work beautifully. There are actually up to 6 "slots", so I'm going to play around with both solutions and see which one works for what I need. They actually gave me some great ideas to improve the rest of the project. I know it wasn't just a quick answer, so I appreciate your time...

              tomhath, I forwarded your link on to my brother who is working on his PHD in Mathematics...I represent the artistic side of my family. :-)

                jsut test which one of the two is the faster one!
                if you do that, post the result ... i wonder who'll win 😉

                In fact, it was not an easy problem. But i really wanted to see if i can solve it scaleable. So, no wasted time and learned a lot ...

                  This can also be solved recursively:

                  <?php 
                  
                  permute("ABCDEF");
                  
                  
                  function permute($str, $temp="") {
                    if ($str == "") {
                      print $temp . "\n";
                    } else {
                      for ($i =0; $i<strlen($str); $i++) {
                        $start = $temp . substr($str, $i, 1);
                        $rest = substr($str, 0, $i) . substr($str, $i+1);
                        permute($rest, $start);
                      }
                    }
                  }
                  ?>
                  

                  To add/remove letters, just change the argument passed to permute and that's it.

                  Diego

                    @: nice and short solution 🙂
                    guess i was too busy with thinking about using an array 😉

                      Thanks 🙂...I don't like arrays, coz it's such a hassle to use them coz u have to declare them the right size, or expand it to allocate more entries, etc...Well, I guess it's easier in PHP, but in other languages like C++ and Java it is a hassle 🙂 Strings are more flexible (well, except C-string, sigh) 🙂

                      Diego

                        Write a Reply...