开发者

array manipulation help in php making phrases

I will have four word list arrays.

$array1 = 开发者_开发技巧array('red','blue','green');//it can have more elements

$array2 = array('ball','radio','bat');

$array3 = array('free','$10','bonus','2free');

$array4 = array('Ny','california');

using these 4 arrays of words which can have 0 to any no of elements in them for example above the out put needs to be

red ball free Ny

red ball free california

red ball $10 Ny

red ball $10 california

red ball bonus Ny

red ball bonus california

red ball 2free Ny

red ball 2free california

red radio free Ny

..................................similarly for all elements possibly in array

it was easy for me up to 2 but with 4 arrays i am a bit confused to achieve it. please help


The simplest way is with nested loops:

$array1 = array('red','blue','green');
$array2 = array('ball','radio','bat');
$array3 = array('free','$10','bonus','2free');
$array4 = array('Ny','california');

for ($i = 0, $maxi = count($array1); $i < $maxi; $i++) {
    for ($j = 0, $maxj = count($array2); $j < $maxj; $j++) {
        for ($k = 0, $maxk = count($array3); $k < $maxk; $k++) {
            for ($l = 0, $maxl = count($array4); $l < $maxl; $l++) {
                echo '<p>' . $array1[$i] . ' ' . $array2[$j] . ' ' . $array3[$k] . ' ' . $array4[$l] . '</p>';
            }
        }
    }
}


foreach($array1 as $first)
  foreach($array2 as $second)
    foreach($array3 as $third)
      foreach($array4 as $fourth)
      {
        echo $first." ".$second." ".$third." ".$fourth;
      }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜