-2

I have two array like these:

$a = array("abc","defs","ghi");
$b = array("abcs","def","ghis");

I want all the combination of strings like these:

abc defs ghi
abc def ghi
abc def ghis
abc defs ghis
abcs defs ghi
abcs def ghi
abcs def ghis
abcs defs ghis

How to do this in php?

PS - array can be of any length but both arrays size are always same.

PPS - The accepted answer given in this question is not giving me the correct result. It is giving the following result:

Array ( [0] => Array ( [0] => abc [1] => abcs ) [1] => Array ( [0] => abc [1] => def ) [2] => Array ( [0] => abc [1] => ghi ) [3] => Array ( [0] => defs [1] => abcs ) [4] => Array ( [0] => defs [1] => def ) [5] => Array ( [0] => defs [1] => ghi ) [6] => Array ( [0] => ghi [1] => abcs ) [7] => Array ( [0] => ghi [1] => def ) [8] => Array ( [0] => ghi [1] => ghi ) ) 
shivank01
  • 1,015
  • 3
  • 16
  • 35
  • Do you mean like https://stackoverflow.com/questions/8567082/how-to-generate-in-php-all-combinations-of-items-in-multiple-arrays – The fourth bird Jul 01 '19 at 10:17
  • Step 1 merge both arrays since your logic needs all of them anyway. Step 2 run a recursive function to generate combinations. Look in to this answer here: https://stackoverflow.com/questions/19067556/php-algorithm-to-generate-all-combinations-of-a-specific-size-from-a-single-set – Auris Jul 01 '19 at 10:18
  • @Auris The order is important for me. So, I can't merge the array. – shivank01 Jul 01 '19 at 10:19
  • In that case look in to the answer linked by The fourth bird. It will get you started. – Auris Jul 01 '19 at 10:21
  • @Auris Please see the updated question. – shivank01 Jul 01 '19 at 10:27
  • @Thefourthbird It is not working for me – shivank01 Jul 01 '19 at 10:27

1 Answers1

1

You need to convert your array to this,

$a    = ["abc", "defs", "ghi"];
$b    = ["abcs", "def", "ghis"];
$temp = array_map(null, $a, $b); // this conversion we call it transposing of array
function combinations($arrays)
{
    $result = [];
    $arrays = array_values($arrays);
    $sizeIn = sizeof($arrays);
    $size   = $sizeIn > 0 ? 1 : 0;
    foreach ($arrays as $array) {
        $size = $size * sizeof($array);
    }
    for ($i = 0; $i < $size; $i++) {
        $result[$i] = [];
        for ($j = 0; $j < $sizeIn; $j++) {
            array_push($result[$i], current($arrays[$j]));
        }
        for ($j = ($sizeIn - 1); $j >= 0; $j--) {
            if (next($arrays[$j])) {
                break;
            } elseif (isset($arrays[$j])) {
                reset($arrays[$j]);
            }
        }
    }
    return $result;
}
$res = combinations($temp);
// imploding all the values internally with space
$temp = array_map(function($item){
    return implode(" ", $item);
},$res);
// looping to show the data
foreach($temp as $val){
    echo $val."\n";
}

Once you convert your array using array_map, then I used help of this.

Demo.

Output

abc defs ghi
abc defs ghis
abc def ghi
abc def ghis
abcs defs ghi
abcs defs ghis
abcs def ghi
abcs def ghis
Rahul
  • 18,271
  • 7
  • 41
  • 60