PHP Extended Array Combinations

It’s hard for me to explain what I want here. So I’ll just try to explain this with code:

// Example 1

$numbers = [1,2,3,4,5,6];

$in_each = 2;

$combinations = superFunction($numbers, $in_each);

// Result

$combinations = [
    [
        [1,2],
        [3,4],
        [5,6]
    ],
    [
        [1,3],
        [2,4],
        [5,6]
    ],
    [
        [1,4],
        [2,3],
        [5,6]
    ]
    // and so on
];


// Example 2

$numbers = [1,2,3,4,5,6];

$in_each = 3;

$combinations = superFunction($numbers, $in_each);

// Result

$combinations = [
    [
        [1,2,3],
        [4,5,6]
    ],
    [
        [1,2,4],
        [3,5,6]
    ]
    // and so on
];

This is a superfunction. I need help creating. Note that the $ in_each variable is used here .

The function does not have to be over-efficient or even fault tolerant. I just need something to get me started.

I saw many different “combo” scripts here, but none of them have the ability to “group them” in this way.

+4
source share
1 answer

: , array_chunk , , . , . , array_chunk($numbers, $in_each);

array_slice, , , . , $numbers , $in_each.

:

$numbers = [1,2,3,4,5,6]
$in_each = 2;
$combinations = array_slice($nums, 0, 0 + $in_each);

$combinations , , :

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

, superFunction , array_slice n , n - length of $numbers, $in_each. n , $in_each. n : 0 2 4.

+2
source

Source: https://habr.com/ru/post/1529405/


All Articles