Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8795101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:20:30+00:00 2026-06-13T23:20:30+00:00

I need a function which returns all possible combinations, e.g. chars = range(‘a’, ‘c’);

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:20:31+00:00Added an answer on June 13, 2026 at 11:20 pm

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a function which finds the possible fixed length combinations of
I need a function which returns true if the certificate of a secure website
I need a function which will check for the existing URLs in a string.
I need to create a function which rounds decimal numbers like this: Round($32.95, 0)
I need to write a TSQL user defined function which will accept a string
I need to implement a simp^le find function which will retrieve the 1st occurence
I often need to use a function which performs and action X is condition
In my Application I need to call a function(Which update the valuesof textviews),I need
I need to be able to create a seperate function which creates a total
I am working with Silverlight Unit Testing i need to test a function which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.