Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4603966
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:13:39+00:00 2026-05-22T00:13:39+00:00

For two days I’ve been running crazy trying to accomplish this, maybe you can

  • 0

For two days I’ve been running crazy trying to accomplish this, maybe you can enlighten me. This is for a horse betting permutation. Every time a user plays, I get a multidimensional array (2 levels). The first level contains the race ID, the the second level contains thee horses selected by the user for that race. It looks like this:

$play = array
(
    '4' => array(7, 32),
    '8' => array(4),
    '2' => array(9),
    '12' => array('5'),
    '83' => array('10', '11', '12', ''),
    '9' => array('3'),
);

I need to know what are all the possible combinations for that play. Which is easily done with this function:

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

This returns an array with all the possible combinations beautifully. So far so good, and the result looks like this:

Array
(
    [0] => Array
        (
            [0] => 7
            [1] => 4
            [2] => 9
            [3] => 5
            [4] => 10
            [5] => 3
        )

    [1] => Array
        (
            [0] => 7
            [1] => 4
            [2] => 9
            [3] => 5
            [4] => 11
            [5] => 3
        )

    [2] => Array
        (
            [0] => 7
            [1] => 4
            [2] => 9
            [3] => 5
            [4] => 12
            [5] => 3
        )

    [3] => Array
        (
            [0] => 32
            [1] => 4
            [2] => 9
            [3] => 5
            [4] => 10
            [5] => 3
        )

    [4] => Array
        (
            [0] => 32
            [1] => 4
            [2] => 9
            [3] => 5
            [4] => 11
            [5] => 3
        )

    [5] => Array
        (
            [0] => 32
            [1] => 4
            [2] => 9
            [3] => 5
            [4] => 12
            [5] => 3
        )

)

My problem: I need the array “key” for every horse to be the “race ID”, not 0,1,2,3. I need the result to be like this:

Array
(
    [0] => Array
        (
            [4] => 7
            [8] => 4
            [2] => 9
            [12] => 5
            [83] => 10
            [9] => 3
        )

    [1] => Array
        (
            [4] => 7
            [8] => 4
            [2] => 9
            [12] => 5
            [83] => 11
            [9] => 3
        )

    [2] => Array
        (
            [4] => 7
            [8] => 4
            [2] => 9
            [12] => 5
            [83] => 12
            [9] => 3
        )

    [3] => Array
        (
            [4] => 32
            [8] => 4
            [2] => 9
            [12] => 5
            [83] => 10
            [9] => 3
        )

    [4] => Array
        (
            [4] => 32
            [8] => 4
            [2] => 9
            [12] => 5
            [83] => 11
            [9] => 3
        )

    [5] => Array
        (
            [4] => 32
            [8] => 4
            [2] => 9
            [12] => 5
            [83] => 12
            [9] => 3
        )

)

How can I accomplish this? I know its a long post but I needed to graph this. I am having problems to wrap my head around the function recursion and I get totally lost in each loop.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:13:40+00:00Added an answer on May 22, 2026 at 12:13 am

    Here’s what you need. I have commented as necessary:

    function permutations(array $array)
    {
        switch (count($array)) {
            case 1:
                // Return the array as-is; returning the first item
                // of the array was confusing and unnecessary
                return $array;
                break;
            case 0:
                throw new InvalidArgumentException('Requires at least one array');
                break;
        }
    
        // We 'll need these, as array_shift destroys them
        $keys = array_keys($array);
    
        $a = array_shift($array);
        $k = array_shift($keys); // Get the key that $a had
        $b = permutations($array);
    
        $return = array();
        foreach ($a as $v) {
            if(is_numeric($v))
            {
                foreach ($b as $v2) {
                    // array($k => $v) re-associates $v (each item in $a)
                    // with the key that $a originally had
                    // array_combine re-associates each item in $v2 with
                    // the corresponding key it had in the original array
                    // Also, using operator+ instead of array_merge
                    // allows us to not lose the keys once more
                    $return[] = array($k => $v) + array_combine($keys, $v2);
                }
            }
        }
    
        return $return;
    }
    

    See it in action.

    By the way, calculating all the permutations recursively is neat, but you might not want to do it in a production environment. You should definitely consider a sanity check that calculates how many permutations there are and doesn’t allow processing to continue if they are over some limit, at the very least.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been attempting this for two days, and constantly running into dead ends. I've
Its been two days and I still can't figure this out: how do I
I've been working at this for two days and have not had much luck.
This has been bugging me for more than two days now, so i thought
Two days ago I wrote this question: How can I retrieve an object on
I've searched for two days trying to find a solution to this. My problem
I've spent two days on this and I still can't find the cause of
I've spent two days on this and have gotten nowhere. I'm trying to use
been working on this for two days now and seem to be getting nowhere.
It has been two days and I haven't been able to make this application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.