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!

Nirmal
  • 9,391
  • 11
  • 57
  • 81
  • 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. – Stefan Gehrig Oct 18 '11 at 09: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). – yossi Oct 18 '11 at 09:19
  • It looks quiet too simple. Is your question written correctly and with all requirements? – Mailo Světel Oct 18 '11 at 09:25
  • @Stefan Gehrig - Thanks. I have now added details to the question. – Nirmal Oct 18 '11 at 09:28
  • I think this guy is taking the same class as this guy: http://stackoverflow.com/questions/7804693/small-sorting-algorithm-in-php – Paul Dixon Oct 18 '11 at 09:29
  • @Mailo - Yes. All requirements are there. I too thought it's simple but don't know why I couldn't come up with a function. – Nirmal Oct 18 '11 at 09:41

2 Answers2

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

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
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);
Paul
  • 6,572
  • 2
  • 39
  • 51