Debt Consolidation - Business Gifts - Free Myspace Layouts - Freelance Jobs - Business in 2007

PDA

View Full Version : I need help with creating a script that mixs up numbers togethier.


exodus
Jun 21st 2007, 6:35 pm
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.

IProx
Jun 21st 2007, 8:08 pm
Is this a probability formula you are looking for?

SeLfkiLL
Jun 21st 2007, 8:24 pm
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;
}

Prookle
Jun 21st 2007, 8:29 pm
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);
?>




Here is the output for this example...


Array
(
[0] => Array
(
[0] => 1
[1] => 91
)

[1] => Array
(
[0] => 1
[1] => 98
)

[2] => Array
(
[0] => 1
[1] => 90
)

[3] => Array
(
[0] => 1
[1] => 99
)

[4] => Array
(
[0] => 1
[1] => 20
)

[5] => Array
(
[0] => 91
[1] => 1
)

[6] => Array
(
[0] => 91
[1] => 98
)

[7] => Array
(
[0] => 91
[1] => 90
)

[8] => Array
(
[0] => 91
[1] => 99
)

[9] => Array
(
[0] => 91
[1] => 20
)

[10] => Array
(
[0] => 98
[1] => 1
)

[11] => Array
(
[0] => 98
[1] => 91
)

[12] => Array
(
[0] => 98
[1] => 90
)

[13] => Array
(
[0] => 98
[1] => 99
)

[14] => Array
(
[0] => 98
[1] => 20
)

[15] => Array
(
[0] => 90
[1] => 1
)

[16] => Array
(
[0] => 90
[1] => 91
)

[17] => Array
(
[0] => 90
[1] => 98
)

[18] => Array
(
[0] => 90
[1] => 99
)

[19] => Array
(
[0] => 90
[1] => 20
)

[20] => Array
(
[0] => 99
[1] => 1
)

[21] => Array
(
[0] => 99
[1] => 91
)

[22] => Array
(
[0] => 99
[1] => 98
)

[23] => Array
(
[0] => 99
[1] => 90
)

[24] => Array
(
[0] => 99
[1] => 20
)

[25] => Array
(
[0] => 20
[1] => 1
)

[26] => Array
(
[0] => 20
[1] => 91
)

[27] => Array
(
[0] => 20
[1] => 98
)

[28] => Array
(
[0] => 20
[1] => 90
)

[29] => Array
(
[0] => 20
[1] => 99
)

)

exodus
Jun 22nd 2007, 4:18 am
Thank you, i went was racking my brain last night looking for this. :)