Get all combinations of an array

前端 未结 4 744
时光说笑
时光说笑 2021-01-07 10:24

I\'m currently trying to make a function that gets all possible combinations of array values.

I have come up with a non function version but it\'s limited to 3 value

4条回答
  •  轮回少年
    2021-01-07 11:09

    The solution posted by Micky Balladelli almost worked for me. Here is a version that does not duplicate values:

    Function Get-Permutations 
    {
        param ($array_in, $current, $depth, $array_out)
        $depth++
        $array_in = $array_in | select -Unique
        for ($i = 0; $i -lt $array_in.Count; $i++)
        {
            $array_out += ($current+" "+$array_in[$i]).Trim()
            if ($depth -lt $array_in.Count)
            {
                $array_out = Get-Permutations $array_in ($current+" "+$array_in[$i]) $depth $array_out
            }
            else {}
        }
        if(!($array_out -contains ($array_in -Join " "))) {}
        for ($i = 0; $i -lt $array_out.Count; $i++)
        {
            $array_out[$i] = (($array_out[$i].Split(" ")) | select -Unique) -Join " "
        }
        $array_out | select -Unique
    }
    

提交回复
热议问题