I need help with creating a script that mixs up numbers togethier.

Discussion in 'PHP' started by exodus, Jun 21, 2007.

  1. #1
    This is kind of hard to explain.

    For example the list: 1 99 67 34 23

    I would like a script that could take a string of space separated numbers that could be any amount and any numbers inputed and then pair them up with each other, but not itself.

    Output would be like
    1 99
    1 67
    1 34
    1 23
    99 1
    99 67
    99 34
    99 23
    67 1
    and so forth.

    The problem I am having is I can't come up with the formula to take a dynamic set of numbers to pair them up with each other. The above is 5 numbers, but users might enter 3 numbers even up to ten numbers.

    Is it really easy and I am just over looking something?

    I was thinking about using a for loop with a array shift and an array push to be able to do it, but i get stuck thinking about it. I know the number it would produce is the number of numbers times itself and I might should setup the for loop with that number.

    Please, help me figure this out, thanks!

    *No this is not a homework assignment.
     
    exodus, Jun 21, 2007 IP
  2. IProx

    IProx Well-Known Member

    Messages:
    1,749
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    130
    #2
    Is this a probability formula you are looking for?
     
    IProx, Jun 21, 2007 IP
  3. SeLfkiLL

    SeLfkiLL Active Member

    Messages:
    85
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    50
    #3
    This can be done with a loop inside a loop

    
    function MakePairs($num_array) {
        $pairs = array();
        foreach($num_array as $outer) {
            foreach($num_array as $inner) {
                if($outer != $inner)
                    $pairs[] = array($outer, $inner);
            }
        }
        
        return $pairs;
    }
    
    PHP:
     
    SeLfkiLL, Jun 21, 2007 IP
  4. Prookle

    Prookle Guest

    Messages:
    144
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Actually, it was quite easy ;)

    Steve

    
    
    <?php
    
    //Here is the input array of numbers:
    $arrInput = Array(1, 91, 98, 90, 99, 20);
    $arrOutput = Array();
    
    $iIndex = 0;
    for($i = 0; $i < count($arrInput); $i++)
    {
    	for($j = 0; $j < count($arrInput); $j++)
    	{
    		//if the numbers are the not same, add them in the matrix:
    		if ($arrInput[$i] != $arrInput[$j])
    		{
    			$arrOutput[$iIndex][0] = $arrInput[$i];
    			$arrOutput[$iIndex][1] = $arrInput[$j];
    			$iIndex++;
    		}
    	}
    }
    
    print_r($arrOutput);
    ?>
    
    
    PHP:

    Here is the output for this example...

     
    Prookle, Jun 21, 2007 IP
  5. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #5
    Thank you, i went was racking my brain last night looking for this. :)
     
    exodus, Jun 22, 2007 IP