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 6698397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:34:24+00:00 2026-05-26T06:34:24+00:00

Hey there I’ve made a recursive permutation function for class, but I output is

  • 0

Hey there I’ve made a recursive permutation function for class, but I output is less than favorable.
http://codepad.org/DOaMP9oc

function permute($arr) {
    $out = array();
    if (count($arr) > 1) {
        $i = 0;
        foreach($arr as $r => $c) {
            $n = $arr;
            unset($n[$r]);
            $out[$c] = permute($n);
        }
    }
    else
        return array_shift($arr);
    return $out;
}

If input is array(1,2,3,4,5), Output is:

Array
(
    [1] => Array
        (
            [2] => Array
                (
                    [3] => Array
                        (
                            [4] => 5
                            [5] => 4
                        )

                    [4] => Array
                        (
                            [3] => 5
                            [5] => 3
                        )

                    [5] => Array
                        (
                            [3] => 4
                            [4] => 3
                        )

                )
ETC......................

This is all Correct, you can read it like this key.key.key.key.value or 12345,12354,12435

Currently, to convert this output to something readable, I’m using this ugly block of code:
http://codepad.org/qyWcRBCl

foreach($out as $k => $a) 
    foreach($a as $l => $b) 
        foreach ($b as $m => $c) 
            foreach ($c as $n => $d) 
                echo $k.$l.$m.$n.$d.'<br>';

How can I alter my function to eliminate the foreach stack and output in a similar format from permute().

  • 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-26T06:34:25+00:00Added an answer on May 26, 2026 at 6:34 am

    I opted to use the following function:

    function showPerms($a,$i='') {
        if (is_array($a))
            foreach($a as $k => $v) 
                showPerms($v,$i.$k);
        else 
            echo $i.$a."\n";
    }                   
    

    However, I’d still like to do this in a singular recursive function.

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

Sidebar

Related Questions

hey there, Im having problems displaying my results in this program but the program
Hey there. been a busy debugger today. ill give ths short version. ive made
Hey there, I'm writing a template container class and for the past few hours
Hey there, trying to install a ruby gem but I keep getting the following
hey there i want to change some css attributes from javascript but i cant
Hey there, In class we're using the Scite editor with gcc and its a
Hey there, everyone. A really random question, but I'm looking to get into some
hey there! im not really into regular expressions, but i need a simple regex
Hey There, This is a pretty simple problem, but it is my first time
Hey there I have this function: void vdCleanTheArray(int (*Gen_Clean)[25]) { } I wanted to

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.