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.
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:
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...