Page 1 of 1

multidimensional array permutation

Posted: Sat Oct 08, 2016 8:43 am
by adsegzy
Hello Guys,

Am developing a matches permutation betting App/Calculator in PHP. The user will have to enter the 2 possible outcomes on each match (win or not win) and combine as many matches as possible. Also will enter amount per bet (eg $10). Then the app will print out possible outcomes of the matches. I have form fields as below;

[text]
MATCHES ODD1 ODD2
Game 1 1.75 2.25
Game 2 1.20 3.50
Game 3 2.50 4.10
[/text]

I know this is a multidimensional array. The above give 8 ways of combinations.

I want to collect these detail and convert them to a multidimensional array like below;

Code: Select all

$play = array
(
    'Game 1' => array(1.75,	2.25),
    'Game 2' => array(1.20,	3.50),
    'Game 3' => array(2.50,	4.10)
);
I got several php permutation functions like below;

Code: Select all

function permutations(array $array)
{
    switch (count($array)) {
        case 1:
            return $array[0];
            break;
        case 0:
            throw new InvalidArgumentException('Requires at least one array');
            break;
    }

    $a = array_shift($array);
    $b = permutations($array);

    $return = array();
    foreach ($a as $key => $v) {
        if(is_numeric($v))
        {
            foreach ($b as $key2 => $v2) {
                $return[] = array_merge(array($v), (array) $v2);
            }
        }
    }

    return $return;
}
$combos = permutations($play);

echo '<pre>';
print_r($combos);
Show result has

[text]
Array
(
[0] => Array
(
[0] => 1.75
[1] => 1.2
[2] => 2.5
)

[1] => Array
(
[0] => 1.75
[1] => 1.2
[2] => 4.1
)

[2] => Array
(
[0] => 1.75
[1] => 3.5
[2] => 2.5
)
etc
[/text]

MY QUESTIONS ARE
1) Is there anyway i can make it to show as

[text]
Array
(
[1] => Array
(
[Game 1] => 1.75
[Game 2] => 1.2
[Game 3] => 2.5
)

[2] => Array
(
[Game 1] => 1.75
[Game 2] => 1.2
[Game 3] => 4.1
)

[3] => Array
(
[Game 1] => 1.75
[Game 2] => 3.5
[Game 3] => 2.5
)

etc
[/text]

2) Is it possible to have d multiplication of all the odds multiplied by the amount in an array (1.75 * 1.2 * 2.5 * 10 = 52.5) as below;

[text]
[1] => Array
(
[Game 1] => 1.75
[Game 2] => 1.2
[Game 3] => 2.5
) $52.5

[2] => Array
(
[Game 1] => 1.75
[Game 2] => 1.2
[Game 3] => 4.1
) $86.1

etc
[/text]

3) I want array with the least multiplication be echo out anywhere on the page.

Are these possible? If the function above is not the best, it there any better one?

Thanks ahead.

Re: multidimensional array permutation

Posted: Sat Oct 08, 2016 1:42 pm
by Christopher
Yes, start by adding the correct key:

Code: Select all

              $return[$key] = array_merge(array($v), (array) $v2);

Re: multidimensional array permutation

Posted: Sun Oct 09, 2016 10:22 am
by adsegzy
Hello Christopher,

Thanks for your help. But it didnt work. I replace the line of code you give me into my function as in below. but now displayed only 2 array.

Code: Select all

function permutations(array $array)
{
    switch (count($array)) {
        case 1:
            return $array[0];
            break;
        case 0:
            throw new InvalidArgumentException('Requires at least one array');
            break;
    }

    $a = array_shift($array);
    $b = permutations($array);

    $return = array();
    foreach ($a as $key => $v) {
        if(is_numeric($v))
        {
            foreach ($b as $key2 => $v2) {
                $return[] = array_merge(array($v), (array) $v2);
               // $return[] = array_merge(array($v), (array) $v2);
            }
        }
    }

    return $return;
}
$combos = permutations($play);

echo '<pre>';
print_r($combos);
Or where am I suppose to put it? Thanks

Re: multidimensional array permutation

Posted: Sun Oct 09, 2016 4:41 pm
by Christopher
Maybe?

Code: Select all

                $return[$key2] = array_merge(array($v), (array) $v2);