我需要通过递归来简化此方法以消除重复的业务逻辑,但我无法弄清楚如何做到这一点:
public function compute()
{
$ret = array();
foreach ($this->_items as $item) {
$ret[] = array($item);
}
foreach ($this->_items as $item) {
foreach ($this->_items as $item2) {
$tmp = array($item, $item2);
if (count($tmp) === count(array_unique($tmp))) {
$ret[] = $tmp;
}
}
}
foreach ($this->_items as $item) {
foreach ($this->_items as $item2) {
foreach ($this->_items as $item3) {
$tmp = array($item, $item2, $item3);
if (count($tmp) === count(array_unique($tmp))) {
$ret[] = $tmp;
}
}
}
}
return $ret;
}
编辑:
这个方法应该返回数组元素的所有组合,所以如果你有这样的数组:
[a, b, c]
它将返回:
[
[a],
[b],
[c],
[a, b],
[a, c],
[b, a],
[b, c],
[a, b, c],
[a, c, b],
[b, a, c],
[b, c, a],
[c, a, b],
[c, b, a]
]