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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:51:29+00:00 2026-06-02T03:51:29+00:00

Suppose an array is Array ( [0] => 1 [1] => 2 [2] =>

  • 0

Suppose an array is

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

I want to call a function by providing two params (both arrays) with following permutations –

array(1) and array(2,3,4)
array(1,2) and array(3,4)
array(1,2,3) and array (4)
array(1,3) and array(2,4)
array(1,4) and array(2,3)
array(2) and array(1,3,4)
and so on...

Of course the actual arrays will be larger.

  • 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-02T03:51:31+00:00Added an answer on June 2, 2026 at 3:51 am

    I don’t know how that “permutation” is called (it’s not a permutation even probably), but it looked promising to me exploiting the fact that the elements in the set are ordered (if not, there is the order of index, so use index instead of value) so to shift from right to left and combine all left with right combinations.

    This is either possible with recursion or with a stack, I normally prefer the stack.

    The usage as suggested (encapsulating the function call):

    $funky = function($a, $b) {
        printf("(%s) and (%s)\n", implode(',', $a), implode(',', $b));
    };
    
    $paired = function($function) {
        return function(array $array) use ($function) {
            ...
        };
    };
    
    
    $funkyAll = $paired($funky);
    $funkyAll(range(1, 5));
    

    Running this with a sorted (more memory required) stack consuming strategy gives the following output (formatted in columns):

    (1) and (2,3,4,5)    (2,4) and (1,3,5)    (1,4,5) and (2,3)
    (2) and (1,3,4,5)    (2,5) and (1,3,4)    (2,3,4) and (1,5)
    (3) and (1,2,4,5)    (3,4) and (1,2,5)    (2,3,5) and (1,4)
    (4) and (1,2,3,5)    (3,5) and (1,2,4)    (2,4,5) and (1,3)
    (5) and (1,2,3,4)    (4,5) and (1,2,3)    (3,4,5) and (1,2)
    (1,2) and (3,4,5)    (1,2,3) and (4,5)    (1,2,3,4) and (5)
    (1,3) and (2,4,5)    (1,2,4) and (3,5)    (1,2,3,5) and (4)
    (1,4) and (2,3,5)    (1,2,5) and (3,4)    (1,2,4,5) and (3)
    (1,5) and (2,3,4)    (1,3,4) and (2,5)    (1,3,4,5) and (2)
    (2,3) and (1,4,5)    (1,3,5) and (2,4)    (2,3,4,5) and (1)
    

    The exemplary implementation (full source-code as gist) is memory optimized and produces this order (array_pop instead of array_shift):

    (1) and (2,3,4,5)    (2,4) and (1,3,5)    (1,4,5) and (2,3)
    (2) and (1,3,4,5)    (2,5) and (1,3,4)    (1,3,4) and (2,5)
    (3) and (1,2,4,5)    (2,4,5) and (1,3)    (1,3,5) and (2,4)
    (4) and (1,2,3,5)    (2,3,4) and (1,5)    (1,3,4,5) and (2)
    (5) and (1,2,3,4)    (2,3,5) and (1,4)    (1,2,3) and (4,5)
    (4,5) and (1,2,3)    (2,3,4,5) and (1)    (1,2,4) and (3,5)
    (3,4) and (1,2,5)    (1,2) and (3,4,5)    (1,2,5) and (3,4)
    (3,5) and (1,2,4)    (1,3) and (2,4,5)    (1,2,4,5) and (3)
    (3,4,5) and (1,2)    (1,4) and (2,3,5)    (1,2,3,4) and (5)
    (2,3) and (1,4,5)    (1,5) and (2,3,4)    (1,2,3,5) and (4)
    

    Implementation:

    $stack[] = array(array(), $array);
    while (list($left, $right) = array_pop($stack)) {
        $min = end($left);
        foreach ($right as $value)
        {
            if ($value < $min) continue;
            $left2 = array_merge($left, array($value));
            $right2 = array_diff($right, $left2);
            if (!($left2 && $count = count($right2))) continue;
            $function($left2, $right2);
            --$count && $stack[] = array($left2, $right2);
        }
    }
    

    I hope this is useful.

    Another array related algorithm: Sorting with a modulus (just for reference, while writing this one I was reminded to that matrix thing)

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

Sidebar

Related Questions

Suppose I have two arrays like the following: $arr2 = array(array(first, second), array(third, fourth));
Suppose I have an array like this: $array = array(a,b,c,d,a,a); and I want to
I want to store several different methods in an array in Ruby. Suppose I
Suppose I have some pointer, which I want to reinterpret as static dimension array
Suppose I have an array of values [a,b,c,d,...] and a function f(x,...) which returns
Suppose I declared an array as following: int myArr = [someSize]; Now I put
Suppose I've the following function: function mul() { return array_reduce(func_get_args(), '*'); } Is is
For example suppose I have a Car object and I want my array to
Suppose I want to construct something like Array[#1^#2 == 3 &, {3, 3}] And
I want to select some data from db and store in an array. Suppose

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.