2

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!

6
  • 2
    What did you achieve so far? Showing some of the relevant code will surely help you getting good answers. Don't expect that a lot of users will provide you with ready-to-use algorithms if you don't show that you already thought about the problem. Commented Oct 18, 2011 at 9:16
  • i am not sure that i understand what are you trying to do, but the solutions seems like a classic recursion. just walk-through the arrays layer by layer, building the new structure (that is because of the unknown number and depth of array elements). Commented Oct 18, 2011 at 9:19
  • It looks quiet too simple. Is your question written correctly and with all requirements? Commented Oct 18, 2011 at 9:25
  • @Stefan Gehrig - Thanks. I have now added details to the question. Commented Oct 18, 2011 at 9:28
  • I think this guy is taking the same class as this guy: stackoverflow.com/questions/7804693/… Commented Oct 18, 2011 at 9:29

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

What??! Is that it? Oh goodness! Thanks Felix, for that quick solution.
Well, I don't know if you really want to get an array of arrays, but yeah, that is one way to solve it. You're welcome :)
1

Non-recursive version. This should run fast!

$result = end($data);

if ($result === false)
{
   return false; // or Array or what makes sense for an empty array.
}

$higherArr = prev($data);

while ($higherArr !== false)
{
   // Set the orignal array to be the one that you built previously.
   $orig = $result;
   $result = array();

   foreach ($higherArr as $higherKey)
   {
      $result[$higherKey] = $orig;
   }

   $higherArr = prev($data);
}

echo 'Finished with: ' . var_export($result, true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.