Mr B
Mr B

Reputation: 4130

How to return permutations of an array in PHP?

I have a function that take a one dimensional array and returns all the possible permutations of the elements within 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, however the second time it is called, it also includes elements from the first array. I can't figure out why this is.

Appreciate the help.

Upvotes: 1

Views: 2992

Answers (3)

Julien Bourdon
Julien Bourdon

Reputation: 1723

I know this question is old, but here's an alternative solution passing the return 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;
    }
}

Upvotes: 2

Qcho
Qcho

Reputation: 1

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);

Upvotes: 0

Gaurav
Gaurav

Reputation: 28755

Because you are using static $permuted_array;

you must 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(); will not set its value to array() after 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);

one more parameter is added $isNew. Send true if you want to get result for new array.

Upvotes: 2

Related Questions