如何在 PHP 中返回数组的排列?

发布于 2024-10-19 22:34:43 字数 893 浏览 2 评论 0原文

我有一个函数,它接受一维数组并返回数组中元素的所有可能的排列;

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

第一次调用该函数时,它会按预期工作,但是第二次调用它时,它还包含第一个数组中的元素。我不明白这是为什么。

感谢您的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

裸钻 2024-10-26 22:34:43

因为您使用的是 static $permuted_array;,所以

您必须使用

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()第一次迭代后,

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

又添加了一个参数 $isNew。如果您想获取新数组的结果,请发送 true。

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.

离笑几人歌 2024-10-26 22:34:43

我知道这个问题已经很老了,但这里有一个替代解决方案,通过引用传递返回数组:

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

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;
    }
}
纵山崖 2024-10-26 22:34:43

固定版本,

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

您可以将其用作
$perms = array_2D_permute($arr);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文