0

I have a array like following...

array(2) {
  ["gender"]=>
   array(2) {
    [1]=>
    string(4) "Male"
    [2]=>
    string(6) "Female"
  }
  ["agegroup"]=>
  array(3) {
    [3]=>
    string(7) "18 - 24"
    [4]=>
    string(7) "25 - 40"
    [5]=>
    string(7) "40 - 65"
  }
}

This array is dynamic. As like 'gender' and 'age group' there can be any number of items. all these items are sub arrays (associative). I want to write a loop in php which loops from first array element against each in further array elements.

In the above example... the following output should come...
Male- 18-24
Male- 25-40
Male- 40-65

FeMale- 18-24
FeMale- 25-40
FeMale- 40-65

If array is like following...

array(2) {
  ["location"]=>
   array(2) {
    [A]=>
    string(4) "New York"
    [B]=>
    string(6) "London"
  }
  ["gender"]=>
   array(2) {
    [1]=>
    string(4) "Male"
    [2]=>
    string(6) "Female"
  }
  ["agegroup"]=>
  array(3) {
    [3]=>
    string(7) "18 - 24"
    [4]=>
    string(7) "25 - 40"
    [5]=>
    string(7) "40 - 65"
  }
}

then... output should be...

New York- Male- 18-24
New York- Male- 25-40
New York- Male- 40-65

New York- FeMale- 18-24
New York- FeMale- 25-40
New York- FeMale- 40-65

London- Male- 18-24
London- Male- 25-40
London- Male- 40-65

London- FeMale- 18-24
London- FeMale- 25-40
London- FeMale- 40-65

If the array length is defined I will be able to use foreach() to write snippet. But the array length of both parent and sub array is dynamic... can some one give me a hint of how to loop it to get the desired output ?

2
  • The keyword you're looking for is "cartesian product", and there have been lots, and lots of questions asking for the same over time. Commented Jun 22, 2015 at 20:03
  • @salathe Realy, duplicate. But too complex solutions are there :) Commented Jun 22, 2015 at 20:12

1 Answer 1

3
function make ($arr, $pref = '') {            // pref - saving path to this point
 foreach (array_shift($arr) as $item)         // take the 1st item of array and remove it
   if($arr) make($arr,$pref . $item ." - ");  // Call with sub-tree, add item to path
   else echo $pref . $item ."\n";             // Empty array - we are at leaf
}

make($arr);

for 2nd case result:

New York - Male - 18 - 24
New York - Male - 25 - 40
New York - Male - 40 - 65
New York - Female - 18 - 24
New York - Female - 25 - 40
New York - Female - 40 - 65
London - Male - 18 - 24
London - Male - 25 - 40
London - Male - 40 - 65
London - Female - 18 - 24
London - Female - 25 - 40
London - Female - 40 - 65
Sign up to request clarification or add additional context in comments.

9 Comments

The OP barely understands loops, so you give them recursion?
There will come time when he has to understand recursion, why not now ?
he writes about diff amount of levels
@Viral because most people learn to walk before they run. Plus iteration is nearly always faster than recursion.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.