Home > Backend Development > PHP Tutorial > How can I generate the Cartesian product of multiple arrays in PHP while eliminating repetitions?

How can I generate the Cartesian product of multiple arrays in PHP while eliminating repetitions?

DDD
Release: 2024-11-26 03:19:09
Original
290 people have browsed it

How can I generate the Cartesian product of multiple arrays in PHP while eliminating repetitions?

Calculating the Cartesian Product of Multiple Arrays in PHP

Problem:

Given multiple PHP arrays, how can we generate a Cartesian product that combines all possible combinations of elements from each array, excluding repetitions?

Solution:

To obtain a Cartesian product in PHP, we can define a recursive function called array_cartesian. This function takes an array of input arrays as arguments. The base case occurs when the number of arrays is zero, in which case an empty array inside an array is returned. Otherwise, the function uses the array_shift function to remove the first array from the list, and then recursively calls itself with the remaining arrays.

For each element v in the first array, the function iterates through each sub-array p in the Cartesian product of the remaining arrays. It then constructs a new sub-array by merging v with p, and adds this sub-array to the resulting Cartesian product.

Example:

Consider the following set of arrays:

$array[0][0] = 'apples';
$array[0][1] = 'pears';
$array[0][2] = 'oranges';

$array[1][0] = 'steve';
$array[1][1] = 'bob';
Copy after login

By passing these arrays to the array_cartesian function, we can obtain the Cartesian product:

$cross = array_cartesian(
    array('apples', 'pears',  'oranges'),
    array('steve', 'bob')
);

print_r($cross);
Copy after login

which produces the following output:

Array
(
    [0] => Array
        (
            [0] => apples
            [1] => steve
        )

    [1] => Array
        (
            [0] => apples
            [1] => bob
        )

    [2] => Array
        (
            [0] => pears
            [1] => steve
        )

    [3] => Array
        (
            [0] => pears
            [1] => bob
        )

    [4] => Array
        (
            [0] => oranges
            [1] => steve
        )

    [5] => Array
        (
            [0] => oranges
            [1] => bob
        )

)
Copy after login

Thus, the function effectively generates all unique combinations of elements from the input arrays.

The above is the detailed content of How can I generate the Cartesian product of multiple arrays in PHP while eliminating repetitions?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template