0

I'm trying to figure out how to combine two arrays while keeping the order of each separate array in PHP.

Here is a example

$array1=array("a","b","c","d"); a needs to be before b, b needs to be before c, c needs to be before d $array2=array("h","g","f","e"); h needs to be before g, g needs to be before f,f needs to be before e

All elements needs to be there so the output will always contain the sum of all elements.

In the end, I'd like to get all combos like:

  • h,g,f,e,a,b,c,d
  • h,g,f,a,e,b,c,d
  • h,g,f,a,b,e,c,d
  • h,g,f,a,b,c,e,d
  • h,g,f,a,b,c,d,e

  • h,g,a,f,e,b,c,d
  • h,g,a,f,b,e,c,d
  • h,g,a,f,b,c,e,d
  • h,g,a,f,b,c,d,e

  • h,g,a,b,f,e,c,d
  • h,g,a,b,f,c,e,d
  • h,g,a,b,f,c,d,e

...etc...

I have found many examples for combinations on this site but none where the output should keep the initial order.

Do you have any idea of how I might do it? I can do it iteratively but given all the combinations, it's clearly not the best way to go.

Thanks Laurent

2
  • 2
    I don't really understand your question. You want to keep the order but you also want every possible order combination? Commented Jan 18, 2015 at 20:27
  • I'm looking to mix arrays with all possible combinations but the order of the initial array should be respected. Hellcode here below has found a solution. Thanks Commented Jan 19, 2015 at 19:06

1 Answer 1

1

Here is my suggestion for a script:

<?php

  $array1 = array("a","b","c","d");
  $array2 = array("h","g","f","e");
  // produce 8-byte-string with each 4 values of array 1 and 2
  // looks like a byte with 8 bits of value 0 and 1
  // and delete combinations with other than 4 zeroes and 4 ones
  $combos = array();
  for($i=0; $i<256; $i++) {
    $str = decbin($i);
    $str = substr("00000000",0,8-strlen($str)).$str;
    // just check if there are 4 ones in the string
    if(strlen(str_replace("0","",$str))==4) {
      // then add it to combos array
      $combos[$str] = array();
      // echo it if you like
      //echo "<br>".$i.": ".$str;
    }
  }
  # now you have all combinations
  # so you can fill it with the array values
  foreach($combos as $key => $array) {
    $array = array();
    $x1=0;
    $x2=0;
    for($i=0; $i<strlen($key); $i++) {
      if(substr($key,$i,1) == "1") {
        $array[] = $array1[$x1];
        $x1++;
      } else {
        $array[] = $array2[$x2];
        $x2++;
      }
    }
    // echo it if you like
    echo "<br>".$key.": ".implode(", ",$array);
    $combos[$key] = $array;
  }
  echo "<br /><br />Combinations: ".count($combos);

?>

And here is the output:

00001111: h, g, f, e, a, b, c, d
00010111: h, g, f, a, e, b, c, d
00011011: h, g, f, a, b, e, c, d
00011101: h, g, f, a, b, c, e, d
00011110: h, g, f, a, b, c, d, e
00100111: h, g, a, f, e, b, c, d
00101011: h, g, a, f, b, e, c, d
00101101: h, g, a, f, b, c, e, d
00101110: h, g, a, f, b, c, d, e
00110011: h, g, a, b, f, e, c, d
00110101: h, g, a, b, f, c, e, d
00110110: h, g, a, b, f, c, d, e
00111001: h, g, a, b, c, f, e, d
00111010: h, g, a, b, c, f, d, e
00111100: h, g, a, b, c, d, f, e
01000111: h, a, g, f, e, b, c, d
01001011: h, a, g, f, b, e, c, d
01001101: h, a, g, f, b, c, e, d
01001110: h, a, g, f, b, c, d, e
01010011: h, a, g, b, f, e, c, d
01010101: h, a, g, b, f, c, e, d
01010110: h, a, g, b, f, c, d, e
01011001: h, a, g, b, c, f, e, d
01011010: h, a, g, b, c, f, d, e
01011100: h, a, g, b, c, d, f, e
01100011: h, a, b, g, f, e, c, d
01100101: h, a, b, g, f, c, e, d
01100110: h, a, b, g, f, c, d, e
01101001: h, a, b, g, c, f, e, d
01101010: h, a, b, g, c, f, d, e
01101100: h, a, b, g, c, d, f, e
01110001: h, a, b, c, g, f, e, d
01110010: h, a, b, c, g, f, d, e
01110100: h, a, b, c, g, d, f, e
01111000: h, a, b, c, d, g, f, e
10000111: a, h, g, f, e, b, c, d
10001011: a, h, g, f, b, e, c, d
10001101: a, h, g, f, b, c, e, d
10001110: a, h, g, f, b, c, d, e
10010011: a, h, g, b, f, e, c, d
10010101: a, h, g, b, f, c, e, d
10010110: a, h, g, b, f, c, d, e
10011001: a, h, g, b, c, f, e, d
10011010: a, h, g, b, c, f, d, e
10011100: a, h, g, b, c, d, f, e
10100011: a, h, b, g, f, e, c, d
10100101: a, h, b, g, f, c, e, d
10100110: a, h, b, g, f, c, d, e
10101001: a, h, b, g, c, f, e, d
10101010: a, h, b, g, c, f, d, e
10101100: a, h, b, g, c, d, f, e
10110001: a, h, b, c, g, f, e, d
10110010: a, h, b, c, g, f, d, e
10110100: a, h, b, c, g, d, f, e
10111000: a, h, b, c, d, g, f, e
11000011: a, b, h, g, f, e, c, d
11000101: a, b, h, g, f, c, e, d
11000110: a, b, h, g, f, c, d, e
11001001: a, b, h, g, c, f, e, d
11001010: a, b, h, g, c, f, d, e
11001100: a, b, h, g, c, d, f, e
11010001: a, b, h, c, g, f, e, d
11010010: a, b, h, c, g, f, d, e
11010100: a, b, h, c, g, d, f, e
11011000: a, b, h, c, d, g, f, e
11100001: a, b, c, h, g, f, e, d
11100010: a, b, c, h, g, f, d, e
11100100: a, b, c, h, g, d, f, e
11101000: a, b, c, h, d, g, f, e
11110000: a, b, c, d, h, g, f, e

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

1 Comment

hellcode, this is brilliant! Exactly what I need and elegant solution! Thanks a lot

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.