Home > Backend Development > PHP Tutorial > How to Generate All Combinations from Multiple Arrays in PHP Recursively?

How to Generate All Combinations from Multiple Arrays in PHP Recursively?

Linda Hamilton
Release: 2024-12-14 02:09:10
Original
817 people have browsed it

How to Generate All Combinations from Multiple Arrays in PHP Recursively?

Generating Combinations from Multiple Arrays in PHP

Introduction

The task of generating all combinations of items from multiple arrays is often encountered during combinatorial optimization problems. This article presents a recursive solution that addresses the need for a function that can handle a variable number of source arrays.

Problem Statement

Given several arrays with different numbers of elements, find all combinations of items, where the number of combinations equals the product of the number of elements in each array. For instance, given the following arrays:

$arrayA = array('A1','A2','A3');
$arrayB = array('B1','B2','B3');
$arrayC = array('C1','C2');
Copy after login

We aim to generate an array of 18 combinations:

[
    ['A1', 'B1', 'C1'],
    ['A1', 'B1', 'C2'],
    ['A1', 'B2', 'C1'],
    ...
]
Copy after login

Recursive Solution

The following recursive function generates all possible combinations of items:

function combinations($arrays, $i = 0) {
    // If reaching the last array, return the array itself
    if (!isset($arrays[$i])) {
        return array();
    }
    
    // If at the parent array, return the recursive call to the following array
    if ($i == count($arrays) - 1) {
        return $arrays[$i];
    }
    
    // Get combinations from subsequent arrays
    $tmp = combinations($arrays, $i + 1);

    $result = array();

    // Concatenate each array from tmp with each element from $arrays[$i]
    foreach ($arrays[$i] as $v) {
        foreach ($tmp as $t) {
            $result[] = is_array($t) ? 
                array_merge(array($v), $t) :
                array($v, $t);
        }
    }
    
    return $result;
}
Copy after login

Demonstration

The following code demonstrates the usage of the combinations function:

print_r(
    combinations(
        array(
            array('A1','A2','A3'), 
            array('B1','B2','B3'), 
            array('C1','C2')
        )
    )
);
Copy after login

This will output the expected array of 18 combinations.

The above is the detailed content of How to Generate All Combinations from Multiple Arrays in PHP Recursively?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template