I have a function that takes a one-dimensional array and returns all possible permutations of the elements in the array;
function array_2D_permute($items, $perms = array()) { static $permuted_array = array(); if (empty($items)) { $permuted_array[]=$perms; #print_r($new); #print join(' ', $perms) . "\n"; } else { for ($i = count($items) - 1; $i >= 0; --$i) { $newitems = $items; $newperms = $perms; list($foo) = array_splice($newitems, $i, 1); array_unshift($newperms, $foo); array_2D_permute($newitems, $newperms); } return $permuted_array; } } $arr1=array("Architecture","Mexico","Periodicals"); $result1=array_2D_permute($arr1); print_r($result1); $arr2=array("Apple","Boat","Cat"); $result2=array_2D_permute($arr2); print_r($result2);
The first time the function is called, it works as expected, but the second time it is called, it also includes elements from the first array. I canβt understand why this is so.
Rate the help.
Sid
source share