How to return array permutations in PHP?

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.

+1
source share
3 answers

Since you are using static $permuted_array;

you should use

 static $permuted_array; // declare static var, if you don't want to use static property of variable, then why are you using this $permuted_array = array(); // set its value to array() 

using static $permuted_array = array(); array( ) will not be set after the first iteration

 function array_2D_permute($items, $perms = array(), $isNew = false) { static $permuted_array = array(); if($isNew) $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, array(), true); // print_r($result1); $arr2=array("Apple","Boat","Cat"); $result2=array_2D_permute($arr2, array(), true); /// print_r($result2); 

added another parameter $ isNew. Send true if you want to get the result for the new array.

+2
source

I know this question is old, but here is an alternative solution that passes the returned array by reference:

 function pc_permute($items, $perms = array( ), &$return = array()) { if (empty($items)) { $return[] = $perms; } else { for ($i = count($items) - 1; $i >= 0; --$i) { $newitems = $items; $newperms = $perms; list($foo) = array_splice($newitems, $i, 1); array_unshift($newperms, $foo); pc_permute($newitems, $newperms,$return); } return $return; } } 
+2
source

fixed version

 protected function array_2D_permute($items, $perms = array(), $reset = true) { static $permuted_array; if ($reset) { $permuted_array = array(); } if (empty($items)) { $permuted_array[] = $perms; } else { for ($i = count($items) - 1; $i >= 0; --$i) { $newitems = $items; $newperms = $perms; list($foo) = array_splice($newitems, $i, 1); array_unshift($newperms, $foo); $this->array_2D_permute($newitems, $newperms, false); } return $permuted_array; } } 

you can use it as $ perms = array_2D_permute ($ arr);

0
source

All Articles