Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Multidimensional Array Permutation

Featured Replies

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;

 

 

MATCHES ODD1 ODD2
Game 1 1.75 2.25
Game 2 1.20 3.50
Game 3 2.50 4.10

 

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;

$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;

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

 

 

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

 

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

 

 

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

 

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;

 

[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

 

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.

If I understand correctly why not rebuild the array with names?

$return = array();
$game1 = 1.75;
$game2 = 1.2;
$game3 = 2.5;
$total = $game1 * $game2 * $game3 * 10;
$newrow = array('game1' => $game1, 'game2' => $game2, 'game3' => $game3, 'total' => $total);
$return[] = $newrow;

Or am I missing something?

Edited by BrowserBugs

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.