Answer the question
In order to leave comments, you need to log in
Write an algorithm for enumerating options with weight?
Tell me even stupid I don’t know how to do it, there is such an array.
<?php
$a = [
0 => 'a',
1 => 'b',
2 => 'c'
];
a
b
c
ab
ac
bc
abc
Answer the question
In order to leave comments, you need to log in
If permutations are not needed, then everything is simple. Each element is either included in the next option or not. Accordingly, you loop 1 to pow(2, count($arr)). You convert each loop number to binary with the decbin built-in function, reverse the result, and use it to make a new version - if 1 is in the corresponding place in the returned decbin string, then include the element, if 0, then exclude it.
$arr = [0 => 'a', 1 => 'b', 2 => 'c'];
$res = [];
for ($i = 1; $i < pow(2, count($arr)); $i++ ) {
$bin = decbin($i);
$case = "";
foreach(str_split(strrev($bin)) as $ind => $symb) if ($symb == "1") $case .= $arr[$ind];
$res[] = $case;
}
echo implode(", ", $res); // a, b, ab, c, ac, bc, abc
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question