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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:38:54+00:00 2026-05-26T05:38:54+00:00

Sorry if a solution exists anywhere else, but I couldn’t find one. I have

  • 0

Sorry if a solution exists anywhere else, but I couldn’t find one.

I have the following array:

$data = array(
    array('a', 'b', 'c'),
    array('e', 'f', 'g'),
    array('w', 'x', 'y', 'z'),
);

I am trying hard to write a function that will give an array like:

a
    e
        w
        x
        y
        z
    f
        w
        x
        y
        z
    g
        w
        x
        y
        z
b
    e
        w
        x
        y
        z
    f
        w
        x
        y
        z
    g
        w
        x
        y
        z
c
    e
        w
        x
        y
        z
    f
        w
        x
        y
        z
    g
        w
        x
        y
        z

The major problem here is that the number of source arrays and their lengths keep changing always. So a function should be capable of handling any data given to it.

I tried to come up with something like this:

function testfunc($data){
    $arrayDepth = count($data);
    foreach($data as $key=>$d){
        foreach($d as $e){
            echo $e . "\n";
            if($key < $arrayDepth){
                array_shift($data);
                testfunc($data);
            }
        }
    }
}

And the output I got was:

a
e
w
x
y
z
f
g
w
x
y
z
b
w
x
y
z
c
e
f
g
w
x
y
z

I am stuck for almost a day with no proper solution. Any help would be great! Thanks!

  • 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-26T05:38:54+00:00Added an answer on May 26, 2026 at 5:38 am

    Recursion [Wikipedia] is your friend:

    function product($arrays) {
        if(count($arrays) === 1) {
            return $arrays[0];
        }
        else {
            $result = array();
            foreach($arrays[0] as $value) {
                $result[$value] = product(array_slice($arrays, 1));
            }
            return $result;
        }
    }
    

    DEMO

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

Sidebar

Related Questions

Sorry if this has been asked before, but I couldn't find a solution to
i'm sorry i've already asked for this but couldn't find a solution yet :(
sorry i'm asking this again, but I can't find a solution. I know there
Sorry if this is extremely obvious, but I have looked and looked for a
I have a solution that has one class library project, and the others are
sorry for my bad english, I'm french. I'm trying to make a newsletter solution
Sorry for the Windows developers out there, this solution is for Macs only. This
Sorry for the basic question - I'm a .NET developer and don't have much
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry the title isn't more help. I have a database of media-file URLs that

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.