I'am looking for a small script that will take two words from two different arrays and will make combinations of 2. Examples: first array: red, blue, green PHP: second array: cat, elephant, monkey PHP: the result should be like this: red cat red elephant red monkey blue cat blue elephant blue monkey green cat green elephant green monkey PHP: but not combinations like "cat monkey"
following might help.. please make modifications according to your convenience.. <?php $arr1 = array('red', 'blue', 'green'); $arr2 = array('cat', 'elephant', 'monkey'); $arr_perm = get_permutations($arr1, $arr2); print_r($arr_perm); function get_permutations($array1, $array2) { $result = ''; foreach($array1 as $elem1) { foreach($array2 as $elem2) { $result[] = $elem1 . ' ' . $elem2; } } return $result; } ?> PHP:
$colors = array('red', 'blue', 'green'); $animals = array('cat', 'elephant', 'monkey'); foreach ($colors as $color) { foreach ($animals as $animal) { echo $color . " " . $animal . "<br />"; } } PHP: Can't test it but it should work as expected.