R
R
Roman Sarvarov2021-11-17 23:17:17
PHP
Roman Sarvarov, 2021-11-17 23:17:17

How to find all combinations of a multilevel associative array?

Unfortunately, I am weak in combinatorics and need help.

I'm making a sitemap site map, for this I need to calculate all possible combinations of an array of 3 elements (in the future there may be more):

$params = [
    'renovation' => ['none', 'finish'], // отделка квартиры
    'roomCount' => [1, 2, 3, 4, 5], // кол-во комнат
    'type' => ['apartment', 'flat'], // тип недвижимости
];


The value of each element needs to be combined with other keys, this is what I want to get as a result:

$result = [
    ['renovation' => 'none'],
    ['renovation' => 'finish'],
    ['roomCount' => 1],
    ['roomCount' => 2],
    ['roomCount' => 3],
    ['roomCount' => 4],
    ['roomCount' => 5],
    ['type' => 'apartment'],
    ['type' => 'flat'],
    ['renovation' => 'none', 'roomCount' => 1],
    ['renovation' => 'none', 'roomCount' => 2],
    ['renovation' => 'none', 'roomCount' => 3],
    ['renovation' => 'none', 'roomCount' => 4],
    ['renovation' => 'none', 'roomCount' => 5],
    ['renovation' => 'finish', 'roomCount' => 1],
    ['renovation' => 'finish', 'roomCount' => 2],
    ['renovation' => 'finish', 'roomCount' => 3],
    ['renovation' => 'finish', 'roomCount' => 4],
    ['renovation' => 'finish', 'roomCount' => 5],
    ['type' => 'flat', 'roomCount' => 1],
    ['type' => 'flat', 'roomCount' => 2],
    ['type' => 'flat', 'roomCount' => 3],
    ['type' => 'flat', 'roomCount' => 4],
    ['type' => 'flat', 'roomCount' => 5],
    ['type' => 'apartment', 'roomCount' => 1],
    ['type' => 'apartment', 'roomCount' => 2],
    ['type' => 'apartment', 'roomCount' => 3],
    ['type' => 'apartment', 'roomCount' => 4],
    ['type' => 'apartment', 'roomCount' => 5],
    ['type' => 'flat', 'renovation' => 'none'],
    ['type' => 'flat', 'renovation' => 'finish'],
    ['type' => 'apartment', 'renovation' => 'none'],
    ['type' => 'apartment', 'renovation' => 'finish'],
    ['renovation' => 'none', 'roomCount' => 1, 'type' => 'apartment'],
    ['renovation' => 'none', 'roomCount' => 2, 'type' => 'apartment'],
    ['renovation' => 'none', 'roomCount' => 3, 'type' => 'apartment'],
    ['renovation' => 'none', 'roomCount' => 4, 'type' => 'apartment'],
    ['renovation' => 'none', 'roomCount' => 5, 'type' => 'apartment'],
    ['renovation' => 'finish', 'roomCount' => 1, 'type' => 'apartment'],
    ['renovation' => 'finish', 'roomCount' => 2, 'type' => 'apartment'],
    ['renovation' => 'finish', 'roomCount' => 3, 'type' => 'apartment'],
    ['renovation' => 'finish', 'roomCount' => 4, 'type' => 'apartment'],
    ['renovation' => 'finish', 'roomCount' => 5, 'type' => 'apartment'],
    ['renovation' => 'none', 'roomCount' => 1, 'type' => 'flat'],
    ['renovation' => 'none', 'roomCount' => 2, 'type' => 'flat'],
    ['renovation' => 'none', 'roomCount' => 3, 'type' => 'flat'],
    ['renovation' => 'none', 'roomCount' => 4, 'type' => 'flat'],
    ['renovation' => 'none', 'roomCount' => 5, 'type' => 'flat'],
    ['renovation' => 'finish', 'roomCount' => 1, 'type' => 'flat'],
    ['renovation' => 'finish', 'roomCount' => 2, 'type' => 'flat'],
    ['renovation' => 'finish', 'roomCount' => 3, 'type' => 'flat'],
    ['renovation' => 'finish', 'roomCount' => 4, 'type' => 'flat'],
    ['renovation' => 'finish', 'roomCount' => 5, 'type' => 'flat'],
];

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2021-11-18
@megakor

Recursively. The function must receive the already typed element and the remaining parameters. It takes the first parameter and iterates through all its options, plus the empty option (skip this parameter). Adds this to the element and runs recursively from the remaining parameters with the element changed. If there are no more parameters, the function adds the current answer to the result. True, this algorithm will also sort out a completely empty variant []. You will need to check this separately and do not add this element to the result.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question