How to implement the proposals from array?

As you can realize the preparation of these proposals from this array in php
Array
(
[0] => free
[1] => free
[2] => free
[3] => free
[4] => free
[5] => free
[6] => free
[7] => free
[8] => free
[9] => free
[10] => free
[11] => free
[12] => free
[13] => free
[14] => free
[15] => free
[16] => free
)

[1] => Array
(
[0] => gift
[1] => gifts
[2] => gifts
[3] => gifts
[4] => gift
[5] => gifts
[6] => gifts
[7] => gift
[8] => gift
)

[2] => Array
(
[0] => in
[1] => on
)

[3] => Array
(
[0] => Moscow
[1] => Moscow
[2] => Moscow
[3] => Moscow
[4] => Moscow
)
)

free gift in Moscow
free gift in Moscow
free gift in Moscow
free gift to Moscow
free gift in Moscow

free gift for Moscow
free gift for Moscow
free gift for Moscow
free gift to Moscow
free gift for Moscow

...
July 9th 19 at 13:27
1 answer
July 9th 19 at 13:29
Solution
The option of constructing an array:
$words = array(
 array('free', 'free', 'free', 'free', 'free', 'free', 'free', 'free', 'free', 'free', 'free', 'free', 'free', 'free', 'free', 'free', 'free'),
 array('gift', 'gifts', 'gifts', 'gifts', 'gift', 'gifts', 'gifts', 'gift', 'gift'),
 array('in', 'on'),
 array('Moscow', 'Moscow', 'Moscow', 'Moscow', 'Moscow')
);


function vars($w, $i) {
 $res = array();
 if ($i == count($w) - 1) {
 $res = $w[$i];
 } else {
 $variants = vars($w, $i+1);
 foreach ($w[$i] as $v) {
 foreach ($variants as $va) {
 array_push($res, $v.' '.$va);
}
}
}
 return $res;
}


$res = vars($words, 0);
foreach ($res as $v) {
echo($v.'<br>');
}


The variant without building the array:
$words = array(
 array('free', 'free', 'free', 'free', 'free', 'free', 'free', 'free', 'free', 'free', 'free', 'free', 'free', 'free', 'free', 'free', 'free'),
 array('gift', 'gifts', 'gifts', 'gifts', 'gift', 'gifts', 'gifts', 'gift', 'gift'),
 array('in', 'on'),
 array('Moscow', 'Moscow', 'Moscow', 'Moscow', 'Moscow')
);


function vars(&$w, $i=0, $s=") {
 if ($i == count($w)) {
echo($s.'<br>');
 } else {
 foreach ($w[$i] as $v) {
 vars($w, $i+1 $s.' '.$v);
}
}
}

vars($words);
but we have not randomly and all the variations - Carmel_Muell commented on July 9th 19 at 13:32
+ the number of columns is unknown so I need to do something on the cycle but does not occur =( - Carmel_Muell commented on July 9th 19 at 13:35
Updated the script, at the expense of memory consumption I'm not sure. The decision of the vlob. - Daphne_Roob commented on July 9th 19 at 13:38
: May God grant you health 0_0 - Carmel_Muell commented on July 9th 19 at 13:41
Added a second version without the array and consuming large amounts of memory. - Daphne_Roob commented on July 9th 19 at 13:44

Find more questions by tags PHP