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 998409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:11:24+00:00 2026-05-16T07:11:24+00:00

This is my first question here :) I have an array with a number

  • 0

This is my first question here 🙂

I have an array with a number of array children, each with unique values and would like to get all the possible unique combinations of those values.

The number of arrays is known but may change over time.

For example,

array(
  [0] => array([0]=>'blue',[1]=>'red'),
  [1] => array([0]=>'sunny',[1]=>'cloudy'),
  [2] => array([0]=>'sweet',[1]=>'acid');

What should I do to get:

array(
  [0] => array([0]=>'blue',[1]=>'sunny',[2]=>'sweet'),
  [1] => array([0]=>'blue',[1]=>'sunny',[2]=>'acid'),
  [2] => array([0]=>'blue',[1]=>'cloudy',[2]=>'sweet'),
  [3] => array([0]=>'blue',[1]=>'cloudy',[2]=>'acid'),
  [4] => array([0]=>'red',[1]=>'sunny',[2]=>'sweet'),
  [5] => array([0]=>'red',[1]=>'sunny',[2]=>'acid'),
  [6] => array([0]=>'red',[1]=>'cloudy',[2]=>'sweet'),
  [7] => array([0]=>'red',[1]=>'cloudy',[2]=>'acid'));

I’ve tried doing it with nested loops but my logic is not too strong.

Very much appreciated if someone can shed some light

  • 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-16T07:11:25+00:00Added an answer on May 16, 2026 at 7:11 am

    (Note: needs a slight modification to use in PHP < 5.3)

    Do this (example on an online interpreter):

    $f = function () { return func_get_args(); };
    $res = array_outer($f,
        array("blue", "red"),
        array("sunny", "cloudy"),
        array("sweet", "acid"));
    

    The function array_outer, inspired in Mathematica’s Outer, is this:

    /**
     * A generalization of the outer product, forming all the possible
     * combinations of the elements of any number of arrays and feeding
     * them to $f.
     * The keys are disregarded
     **/
    function array_outer($f, array $array1) {
        $res = array();
        $arrays = func_get_args();
        array_shift($arrays);
        foreach ($arrays as $a) {
            if (empty($a))
                return $res;
        }
    
        $num_arrays = count($arrays);
        $pos = array_fill(0, $num_arrays, 0);
        while (true) {
            $cur = array();
            for ($i = 0; $i < $num_arrays; $i++) {
                $cur[] = $arrays[$i][$pos[$i]];
            }
            $res[] = call_user_func_array($f, $cur);
            for ($i = $num_arrays-1; $i >= 0; $i--) {
                if ($pos[$i] < count($arrays[$i]) - 1) {
                    $pos[$i]++;
                    break;
                } else {
                    if ($i == 0)
                        break 2;
                    $pos[$i] = 0;
                }
            }
        }
        return $res;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 503k
  • Answers 503k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer set @override = (coalesce((select override from groupPreferences g inner join… May 16, 2026 at 2:47 pm
  • Editorial Team
    Editorial Team added an answer There are many good answers here but I think a… May 16, 2026 at 2:47 pm
  • Editorial Team
    Editorial Team added an answer I ended up finding "Spring integration" which includes plugins to… May 16, 2026 at 2:47 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

For my first question here I would like to ask you how you'd do
I have an array like this: $sports = array( 'Softball - Counties', 'Softball -
I have this Array i wrote a function MostFreq that takes an array of
here is my problem I have the following array (for example) string[] arr =
First question. Be gentle. I'm working on software that tracks technicians' time spent working
First, apologies,this should be simple but I've had too much coffee and cannot wrap
Here is an example bit from the xml file: <array> <dict> <key>Name</key> <string>Joe Smith</string>
This may be a lame question but I am a total novice with regular
I am not strong on my PHP knowledge, but I have never seen this
Somewhere in my code I have: class aclass { ... function amethod() { $this->dom

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.