Answer the question
In order to leave comments, you need to log in
How to get all possible permutations?
Let's say we have an array
$params = [
'option1' => [1, 2, 3],
'option2' => [1, 2, 3, 4],
'option3' => [1, 2],
];
$result = [
[['option1','1'],],
[['option1','2'],],
[['option1','3'],],
[['option1','1'], ['option1','2'],],
[['option1','1'], ['option1','3'],],
[['option1','2'], ['option1','3'],],
[['option1','1'], ['option1','2'], ['option1','3']],
[['option2', 1],],
[['option2', 2],],
[['option2', 3],],
[['option2', 4],],
[['option2', 1], ['option2', 2],],
[['option2', 1], ['option2', 3],],
[['option2', 1], ['option2', 4],],
[['option2', 2], ['option2', 2],],
[['option2', 2], ['option2', 3],],
[['option2', 2], ['option2', 4],],
[['option2', 3], ['option2', 3],],
[['option2', 3], ['option2', 4],],
[['option2', 4], ['option2', 4],],
[['option2', 1], ['option2', 2], ['option2', 3],],
[['option2', 1], ['option2', 2], ['option2', 4],],
[['option2', 1], ['option2', 3], ['option2', 4],],
[['option2', 1], ['option2', 2], ['option2', 3], ['option2', 4]],
[['option3','1'],],
[['option3','2'],],
[['option3','1'], ['option3','2'],],
// а теперь самое интересное
[['option1','1'], ['option2', 1],],
[['option1','1'], ['option2', 2],],
[['option1','1'], ['option2', 3],],
[['option1','1'], ['option2', 4],],
[['option1','1'], ['option2', 1], ['option2', 2],],
// ...
[['option1','1'], ['option1','2'], ['option2', 1],],
// ...
[['option1','1'], ['option2', 1], ['option3','1'],],
// .. и т д
];
Answer the question
In order to leave comments, you need to log in
Convert the original array to a flat array containing all single values with the check flag added
$paramsFlat = [
['value' => ['option1' => 1], 'check' => 0],
['value' => ['option1' => 2], 'check' => 0],
...
['value' => ['option3' => 2], 'check' => 0],
];
function getNext($paramsFlat) {
foreach($paramsFlat as &$elem) {
// Инвертируем значения до первого найденного нуля
if($elem['checked'] = !$elem['checked']) break;
}
// Получаем массив, содержащий отмеченные элементы
$retVal = array_filter($paramsFlat, function($v) {return $v['checked'];});
if(!count($retVal)) return false;
// Возвращаем массив из значений отмеченных элементов
return array_map(function($v) {return $v['value'];}, $paramsFlat);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question