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

  • SEARCH
  • Home
  • 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 7674389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:43:37+00:00 2026-05-31T16:43:37+00:00

I saw this algorithm that will take numbers or words and find all possible

  • 0

I saw this algorithm that will take numbers or words and find all possible combinations

And I’m using it, but it does NOT return all “real” combinations.

PHP:

<?php
    require_once 'Math/Combinatorics.php';
    $words = array('cat', 'dog', 'fish');
    $combinatorics = new Math_Combinatorics;
    foreach($combinatorics->permutations($words, 2) as $p) {
        echo join(' ', $p), "\n"; 
    }
?>

And it returns:

cat dog
dog cat
cat fish
fish cat
dog fish
fish dog

But these are not all real combinations, all real combinations includes these too:

cat cat
dog dog
fish fish

And that is what I need, the method to get all real combinations:

cat dog
dog cat
cat fish
fish cat
dog fish
fish dog
cat cat
dog dog
fish fish
  • 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-05-31T16:43:38+00:00Added an answer on May 31, 2026 at 4:43 pm

    OK, here’s your code (and btw, thanks for posting such an interesting and challenging problem – at least for me… :-)) – using recursion for all possible permutations (by N) given an array of elements)

    Code :

    <?php
    
    function permutations($arr,$n)
    {
         $res = array();
    
         foreach ($arr as $w)
         {
               if ($n==1) $res[] = $w;
               else
               {
                     $perms = permutations($arr,$n-1);
    
                     foreach ($perms as $p)
                     {
                          $res[] = $w." ".$p;
                     } 
               }
         }
    
         return $res;
    }
    
    // Your array
    $words = array('cat','dog','fish');
    
    // Get permutation by groups of 3 elements
    $pe = permutations($words,3);
    
    // Print it out
    print_r($pe);
    
    ?>
    

    Output :

    Array
    (
        [0] => cat cat cat
        [1] => cat cat dog
        [2] => cat cat fish
        [3] => cat dog cat
        [4] => cat dog dog
        [5] => cat dog fish
        [6] => cat fish cat
        [7] => cat fish dog
        [8] => cat fish fish
        [9] => dog cat cat
        [10] => dog cat dog
        [11] => dog cat fish
        [12] => dog dog cat
        [13] => dog dog dog
        [14] => dog dog fish
        [15] => dog fish cat
        [16] => dog fish dog
        [17] => dog fish fish
        [18] => fish cat cat
        [19] => fish cat dog
        [20] => fish cat fish
        [21] => fish dog cat
        [22] => fish dog dog
        [23] => fish dog fish
        [24] => fish fish cat
        [25] => fish fish dog
        [26] => fish fish fish
    )
    

    HINT : By permutations($words,2), you’ll be able to get exactly what you wanted…

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

Sidebar

Related Questions

I saw this somewhere, but cannot find it now. Is there a built-in function
Saw this questions asked many times. But couldn't find a reasonable answer. What is
I saw this same question for VIM and it has been something that I
I saw this the other day (scroll all the way down to see some
I saw this question , which focuses on the Brittney Spears problem. But I
I saw this list of major version numbers for Java in another post: Java
I saw this answer to a similar question , but it always scares me
I saw this interview question recently for a firm that said: Group of people,
Saw this signature today: public interface ISomeInterface<in T> What impact does the in parameter
I saw this post but didn't understand if this was the same issue. My

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.