2

I need a function which returns all possible combinations,

e.g.

chars = range('a', 'c');

  1. = a a a
  2. = a a b
  3. = a b a
  4. = a b b
  5. = a b c
  6. = a c b ... n. = c c c

(order doesn't matter)

and so on

i got this

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

$p = pc_permute(array(0, 1, 2, 3));
var_dump($p);

from Here

But i wasn't able to figure out how to chance/rewrite this to get all possible combination with multiple same elements.

Thanks, Mohammer

0

1 Answer 1

1

Please use this function:

<?php 
$characters = range('a','c');


function get_permutations(array $arr = array()){
    if(count($arr) == 1){
        return array_values($arr);
    }

    $return_array = array();

    foreach($arr as $key => $val){
        $temp_arr = $arr;
        unset($temp_arr[$key]);
        $temp = call_user_func(__FUNCTION__, $temp_arr);
        for($x = 0; $x < count($temp); $x++){
            $temp[$x] = $val.$temp[$x];
        }
        $return_array = array_merge($return_array, $temp);
    }
    return $return_array;
}

var_dump(get_permutations($characters));

Output:

array(6) {
  [0]=>
  string(3) "abc"
  [1]=>
  string(3) "acb"
  [2]=>
  string(3) "bac"
  [3]=>
  string(3) "bca"
  [4]=>
  string(3) "cab"
  [5]=>
  string(3) "cba"
}

EDIT:

<?php 
$characters = range('a','h');


function get_permutations(array $arr = array(), $max_length = NULL){
    if(count($arr) == 1 || ($max_length !== NULL && $max_length <= 1)){
        return array_values($arr);
    }

    $return_array = array();

    foreach($arr as $key => $val){
        $temp_arr = $arr;
        unset($temp_arr[$key]);
        $temp = call_user_func(__FUNCTION__, $temp_arr, $max_length !== NULL ? $max_length - 1 : NULL);
        for($x = 0; $x < count($temp); $x++){
            $temp[$x] = $val.$temp[$x];
        }
        $return_array = array_merge($return_array, $temp);
    }
    return $return_array;
}

var_dump(get_permutations($characters, 4));

NOTE: Beware using a-z range will lead to a greater runtime or even leads to out of memory error so i tested it with a small range :)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, works aboslutly perfect!! How could I modify this function to get range('a','z'); but with a predefined string-length, e.g. 5chars?
@Mohammer : sorry for l8 response, pass another variable and set the value and test the value to be zero and on each recursion decrease the value, check the edits in some time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.