I want to generate an array of all combinations of an input array of search keywords, I have referred to a Javascript solution Here, and I have to achieve the same kind of implementation in PHP, I have tried like below. I am not able to get the final output when I called. Thanks in advance
function getCombinations($chars){
$result = array();
$f = function ($prefix, $chars, $result) use (&$f) {
for ($i = 0; $i < count($chars); $i++) {
$item = $prefix.' '. $chars[$i];
array_push($result, $item);
return $f($item, array_slice($chars, ($i + 1)), $result);
}
};
$f('', $chars, $result);
return $result;
}
sample array
$chars = array("new", "world", "net", "cafe");
$result = getCombinations($chars);
Expected output
[' new world net cafe', ' new world cafe', ' world net cafe', ' new world net', ' new net cafe', ' world cafe', ' new world', ' world net', ' new cafe', ' net cafe', ' new net', ' world', ' cafe', ' new', ' net']
Update I don't see the expected answer like above from this suggested link here by bill-karwin