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

  • Home
  • SEARCH
  • 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 8726503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:15:33+00:00 2026-06-13T08:15:33+00:00

perhaps I’m having a dumb moment, but I just can’t seem to work this

  • 0

perhaps I’m having a dumb moment, but I just can’t seem to work this out in my head.

I’ll try and explain:-

I have three ‘sets.’ Each set contains 6 objects. I need a loop (or series of nested loops) to result every combination of these sets.

It’s for a game, I’m trying to write a script to compare armor sets – sad I know. The array of sets looks like this:-

class => array(
  set1 => array(
    hat item => array(stats)
    glove item => array(stats)
    ...
  set2 => array(
    hat item => array(stats)
    ...
  set3 => array(
    ...

)

This may be a lot simpler (or a lot more difficult?) than I thought.

Ideas welcome!

Thanks,
Ruu.

EDIT:-

Ok here’s an array sample, I’ve stripped out a LOT of the extra stats, and just left one for each piece:-

$setinfo = array(
    'rk' => array(
        'set1' => array(
            'hat' => array(
                'will' => 114,
            ),
            'shoulders' => array(
                'will' => 78,
            ),
            'shirt' => array(
                'will' => 78,
            ),
            'gloves' => array(
                'will' => 78,
            ),
            'trousers' => array(
                'will' => 78,
            ),
            'boots' => array(
                'will' => 114,
            ),
        ),
        'set2' => array(
            'hat' => array(
                'will' => 78,
            ),
            'shoulders' => array(
                'will' => 78,
            ),
            'shirt' => array(
                'will' => 114,
            ),
            'gloves' => array(
                'will' => 78,
            ),
            'trousers' => array(
                'will' => 114,
            ),
            'boots' => array(
                'will' => 78,
            ),
        ),
        'set3' => array(
            'hat' => array(
                'will' => 78,
            ),
            'shoulders' => array(
                'will' => 114,
            ),
            'shirt' => array(
                'will' => 78,
            ),
            'gloves' => array(
                'will' => 114,
            ),
            'trousers' => array(
                'will' => 78,
            ),
            'boots' => array(
                'will' => 78,
            ),
        ),
    )
);

From this example (I will worry about comparing other stats later,) the ‘ideal’ pieces would be, the hat and boots from set1, with the shirt and trousers from set2, and the gloves and shoulders from set3.

My query is how best to work out a loop which can generate a ‘result’ array with every permutation, for comparison later.

  • 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-06-13T08:15:34+00:00Added an answer on June 13, 2026 at 8:15 am

    Here is an implementation from this guy:

    function array_cartesian_product($arrays)
    
    {
        $result = array();
        $arrays = array_values($arrays);
        $sizeIn = sizeof($arrays);
        $size = $sizeIn > 0 ? 1 : 0;
        foreach ($arrays as $array)
            $size = $size * sizeof($array);
        for ($i = 0; $i < $size; $i ++)
        {
            $result[$i] = array();
            for ($j = 0; $j < $sizeIn; $j ++)
                array_push($result[$i], current($arrays[$j]));
            for ($j = ($sizeIn -1); $j >= 0; $j --)
            {
                if (next($arrays[$j]))
                    break;
                elseif (isset ($arrays[$j]))
                    reset($arrays[$j]);
            }
        }
        return $result;
    }
    $combinations = array_cartesian_product($sets);
    

    Here is a simple test case: http://phpfiddle.org/main/code/m1i-w7m

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

Sidebar

Related Questions

Perhaps I'm just having trouble figuring out the callbackyness, but I can't figure out
Perhaps i am being stupid by asking this just now, but... I notice that
Perhaps this is answered out there somewhere and I just don't know the words
Perhaps I should split this into separate questions, but having tried all of this,
Perhaps I am not getting SQL Parameters correctly, but this doesn't work for me.
Perhaps you can help me find this in the docs. I'm using pound-quote to
Perhaps any of you can help me with this anoying problem. I have long
Perhaps this is an effect of installing the new 4.5 beta, but my simulator
Perhaps I'm not using the right terminology, but I'm struggling to try to find
Perhaps it is a dumb question but I am confused with the following: Are

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.