0

How could I convert this array:

$a = array(
 0 => array(
     0 => 31,
     1 => 39
 ),
 1 => array(
     0 => 41
 )
);

to this one:

   $a = array(
    0 => array(
        0 => array(
            0 => 31,
            1 => 41
        ),
        1 => array(
            0 => 39,
            1 => 41
        )
    ),
    1 => array(
        0 => array(
            0 => 41,
            1 => 31
        ),
        1 => array(
            0 => 41,
            1 => 39
        )
    )
);

I tried several ways, but find no proper solution. At the moment my brain is overheated. So maybe someone has a solution for me.

Thanks @Manos.

Unfortunatly there are dynamic arrays. So these static function wont work for me.

So the array could also look like this:

$a = array(
 0 => array(
     0 => 31,
     1 => 39
 ),
 1 => array(
     0 => 41,
     1 => 49,
     2 => 51
 ),
 2 => array(
     0 => 73
 )
);

Output should look like this:

   $a = array(
        0 => array(
            0 => array(
                0 => 31,
                1 => 41,
                2 => 73
            ),
            1 => array(
                0 => 31,
                1 => 49,
                2 => 73
            ),
            2 => array(
                0 => 31,
                1 => 51,
                2 => 73
            ),
            3 => array(
                0 => 39,
                1 => 41,
                2 => 73
            ),
            4 => array(
                0 => 39,
                1 => 49,
                2 => 73
            ),
            5 => array(
                0 => 39,
                1 => 51,
                2 => 73
        ),
        1 => array(
            0 => array(
                0 => 41,
                1 => 31,
                2 => 73
            ),
            1 => array(
                0 => 41,
                1 => 39,
                2 => 73
            ),
            2 => array(
                0 => 49,
                1 => 31,
                2 => 73
            ),
            3 => array(
                0 => 49,
                1 => 39,
                2 => 73
            ),
                etc ......
        )
    );

Manos function Output:

    array(3) {
  [0]=>
  array(8) {
    [0]=>
    array(2) {
      [0]=>
      int(31)
      [1]=>
      int(41)
    }
    [1]=>
    array(2) {
      [0]=>
      int(39)
      [1]=>
      int(41)
    }
    [2]=>
    array(2) {
      [0]=>
      int(31)
      [1]=>
      int(49)
    }
    [3]=>
    array(2) {
      [0]=>
      int(39)
      [1]=>
      int(49)
    }
    [4]=>
    array(2) {
      [0]=>
      int(31)
      [1]=>
      int(51)
    }
    [5]=>
    array(2) {
      [0]=>
      int(39)
      [1]=>
      int(51)
    }
    [6]=>
    array(2) {
      [0]=>
      int(31)
      [1]=>
      int(73)
    }
    [7]=>
    array(2) {
      [0]=>
      int(39)
      [1]=>
      int(73)
    }
  }
  [1]=>
  array(9) {
    [0]=>
    array(2) {
      [0]=>
      int(41)
      [1]=>
      int(31)
    }
    [1]=>
    array(2) {
      [0]=>
      int(49)
      [1]=>
      int(31)
    }
    [2]=>
    array(2) {
      [0]=>
      int(51)
      [1]=>
      int(31)
    }
    [3]=>
    array(2) {
      [0]=>
      int(41)
      [1]=>
      int(39)
    }
    [4]=>
    array(2) {
      [0]=>
      int(49)
      [1]=>
      int(39)
    }
    [5]=>
    array(2) {
      [0]=>
      int(51)
      [1]=>
      int(39)
    }
    [6]=>
    array(2) {
      [0]=>
      int(41)
      [1]=>
      int(73)
    }
    [7]=>
    array(2) {
      [0]=>
      int(49)
      [1]=>
      int(73)
    }
    [8]=>
    array(2) {
      [0]=>
      int(51)
      [1]=>
      int(73)
    }
  }
  [2]=>
  array(5) {
    [0]=>
    array(2) {
      [0]=>
      int(73)
      [1]=>
      int(31)
    }
    [1]=>
    array(2) {
      [0]=>
      int(73)
      [1]=>
      int(39)
    }
    [2]=>
    array(2) {
      [0]=>
      int(73)
      [1]=>
      int(41)
    }
    [3]=>
    array(2) {
      [0]=>
      int(73)
      [1]=>
      int(49)
    }
    [4]=>
    array(2) {
      [0]=>
      int(73)
      [1]=>
      int(51)
    }
  }
}

1 Answer 1

1
foreach ($a as $first_group_key => $first_group) {
    foreach ($a as $second_group_key => $second_group) {
        if ($second_group_key == $first_group_key) {
            continue;
        }
        $i = count($b[$first_group_key]);
        foreach ($second_group as $second_value) {
            foreach ($first_group as $first_key => $first_value) {
                $b[$first_group_key][$i][0] = $first_value;
                $b[$first_group_key][$i][1] = $second_value;
                $i++;
            }
        }
    }
}
7
  • Thanks Manos. But this doesn't work for me. I've got dynamic arrays (see above for example), so your static function won't work for me. Commented Aug 27, 2013 at 10:37
  • I realised, that it unfortunatly only works if the array has two parent array keys. What I need is a dynamic function no matter how many parent array keys and children values the dynamic array has. Commented Aug 27, 2013 at 14:17
  • you got me curious, so I tried to see how it exactly works. Now it works as you wish! Commented Aug 27, 2013 at 15:56
  • This is a nice solution, but unfortunatly not what i look for (See above for expected example output). For simple explanation: I'm looking for a array solution, which combines every array value width each other array values grouped by array keys. It should not matter, if the count of grouped values is different. So it have to be a dynamic function. Commented Aug 28, 2013 at 5:46
  • what is above is very close to what you want (I think). I suggest you to squeeze your brain a bit to change it accordingly ;) A good idea is to put in your code echo '<pre>'; print_r ($b); echo '</pre>'; to manually see what's being printed! Commented Aug 28, 2013 at 8:13

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.