Recursive Array Exploding

Welcome to Programming Tutorial official website. Today - we are going to cover how to solve / find the solution of this error Recursive Array Exploding on this date .

I have string structured like

(cat,dog,fish) && (drinks) && (milk,water)

I need to convert to an array list like

cat drinks milk
cat drinks water
dog drinks milk
dog drinks water
fish drinks milk
fish drinks water

I’ve thought about doing it with a loop that takes each group and inserts them into an array like

0th pass: fill the array with the first row

(cat,dog,fish) && (drinks) && (milk,water)

1st pass: detect the first group and split it while removing the source

cat && (drinks) && (milk,water) 
dog && (drinks) && (milk,water) 
fish && (drinks) && (milk,water)

2nd pass ….

then loop this each time take the line split it add it to the end and remove the original.

Do you have a better idea? and in PHP?

For those who wonder It is part of sentence parsing code I’m writing.



Once i needed to make every combination of similar sets. I had a recursive function which was actually very resource-intensive on big array (9 parts containing 5 items each) but i can try to adjust it for you:

function combination($string,$level)
  global $output;
  global $input;
  if (isset($input[$level]))
    if (is_array($item))
      foreach ($item as $i)     
        combination($string." ".$i,$level+1);
      combination($string." ".$item,$level+1);

However converting your string into input array is different problem, which i am not sure how to solve so i will keep it up to you.