2

Let's say I have a multidimensional array made up of 3 arrays (1-100 possible) with the following values:

['NAME'] => array(
    [0] => 'Bob',
    [1] => 'Mike'
),
['MIDDLE'] => array(
    [0] => 'goes to',
    [1] => 'hates'
),
['SUBJECT'] => array(
    [0] => 'the movies.',
    [1] => 'the swimmingpool.'
)

Then I want to convert these into 8 arrays with these values:

[0] => array(
    ['NAME'] => 'Bob',
    ['MIDDLE'] => 'goes to',
    ['SUBJECT'] => 'the movies.' 
),
[1] => array(
    ['NAME'] => 'Bob',
    ['MIDDLE'] => 'goes to',
    ['SUBJECT'] => 'the swimmingpool.' 
),
[2] => array(
    ['NAME'] => 'Bob',
    ['MIDDLE'] => 'hates',
    ['SUBJECT'] => 'the movies.' 
),
[2] => array(
    ['NAME'] => 'Bob',
    ['MIDDLE'] => 'hates',
    ['SUBJECT'] => 'the swimmingpool.' 
),
etc.

Can anyone give me a hint in the right direction ? Just can't figure it out.

Falko Woudstra
  • 164
  • 1
  • 10
  • what about nested for loop? – Kurohige Dec 16 '16 at 20:02
  • nested for loop doesn't take into account the possibility of more arrays. I could have 8 values like NAME, MIDDLE and SUBJECT. – Falko Woudstra Dec 16 '16 at 20:03
  • what do you mean with *8 values like NAME ...* ? show a more complex example – RomanPerekhrest Dec 16 '16 at 20:14
  • Permutation, is what you're trying to implement here, which in this case will give you 2^3 = 8 possibilities. [https://en.wikipedia.org/wiki/Permutation](https://en.wikipedia.org/wiki/Permutation) – Rajdeep Paul Dec 16 '16 at 20:18
  • ^ I was just about to ask if you're looking for permutations. If so, then you can loop over all known arrays. – Rob W Dec 16 '16 at 20:21
  • 1
    Possible duplicate of [Get all permutations of a PHP array?](http://stackoverflow.com/questions/10222835/get-all-permutations-of-a-php-array) – Rob W Dec 16 '16 at 20:25
  • if I understood your problem a solution could be this one http://sandbox.onlinephpfunctions.com/code/3035b449ad9af1b48efc2daa6261de0ab5f8eea9 just if there are a limited number of arrays like 3 in your case – Kurohige Dec 16 '16 at 20:27
  • I want something like this but with a multidimensional array. Also the nr of arrays is variable. – Falko Woudstra Dec 16 '16 at 21:11

0 Answers0