Answer the question
In order to leave comments, you need to log in
How to make all possible combinations of elements of different arrays of size N?
Good afternoon!
There are N number of arrays of different sizes. For example:
[A, B, C]
[1, 2, 3, 4, 5, 6]
[X, Y, W, Z]
....
[one, two, three]
It is necessary to iterate over all possible combinations of array elements, but so that the number of elements in the resulting one-dimensional array is not more than 2/3 (how many you specify). In this case, only 1 value is taken from each array.
So, with a limit of 2, it can turn out:
[A, 1]
[A, two]
[C, W]
...
with a limit of 3 elements:
[1, Z, three]
[C, 5, W]
...
that is, it is not necessary that all N arrays participate in the formation of combinations.
Help compose a recursion in php to collect all possible combinations of array elements, provided that each array has its own number of emails and the number of arrays itself can also change
Answer the question
In order to leave comments, you need to log in
I don't know about recursion, but it can be made simpler
. You drive everything into one two-dimensional array, that is, you make a matrix
. You multiply the number of elements of each row by each other.
Those. for example you have three arrays in one 5 in another 4 and in the third 6 elements. it turns out all combinations will be = 5 * 4 * 6 + 4 * 6 = 144 pairs. Although there is probably a formula in mathematics ready. Try reading in combinatorics
First, put the input into a two-dimensional array, or a list of arrays, or whatever php has, so you can take the first, second, and so on. input array by number or move to next.
Further, as you already know, we need a recursive function. Pass it the already assembled part of the answer and which of the arrays should be used next (an iterator in a list or a number in an array).
The function takes one of the elements in the loop from the current array and adds it to the answer and runs recursively from the next array. Also, the function does not take anything from the current array and runs recursively.
You need to insert the following checks: If the function is launched from the last array, then you need to display the current response. The loop over the elements of the current array is skipped if the answer already has the maximum possible number of elements.
A recursive call without adding an element is skipped if there are as many remaining arrays as there are not enough elements in the current response to the minimum required size.
The pseudocode is something like this (not php, so as not to disgrace):
function Generate(result, array_index, arrays) {
if(result >= len(arrays)) {
print(result)
return
}
if (min_length - len(result) < len(arrays)-array_index-1) {
Generate(answer, array_index+1, arrays);
}
if (len(answer) < max_length) {
for i = 0...len(arrays[array_index]) {
answer.push_back(arrays[array_index][i]);
Generate(answer, array_index+1, arrays);
answer.pop_back();
}
}
}
it was not possible to create a universal code right away. I think it is necessary to compose the code dynamically, through create_function ().
For the case of two elements, here is the code:
$sources = [
['A', 'B', 'C',],
[1, 2, 3, 4, 5, 6,],
['X', 'Y', 'W', 'Z'],
['1X', '2Y', '3W', '4Z'],
];
function combine_by_two( $source ) {
$result = [];
// нужно, чтобы было хотя бы 2 массива-источника
while ( count($source) > 1 ) {
/**
* AFAIK, технически легче оперировать с последним элементом, а не первым
* (что привычнее для человека)
*/
$primary = array_pop($source);
$linearizedArray = call_user_func_array("array_merge", $source);
foreach ($primary as $primaryItem) {
foreach ($linearizedArray as $secondaryItem) {
$result[] = [$primaryItem, $secondaryItem];
}
}
// повторяем с усечённым массивом
$result += combine_by_two($source);
}
return $result;
}
$combined = combine_by_two($sources);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question