private function getPossibleCombinations(array $elements, $combinationsLevel)
    {
        $baseCount = count($elements);
        $result    = [];

        if ($combinationsLevel === 1) {
            foreach ($elements as $element) {
                $result[] = [$element];
            }

            return $result;
        }

        $nextLevelElements = $this->getPossibleCombinations($elements, $combinationsLevel - 1);

        foreach ($nextLevelElements as $nextLevelElement) {
            $lastElement = $nextLevelElement[$combinationsLevel - 2];
            $found       = false;

            foreach ($elements as $key => $element) {
                if ($element === $lastElement) {
                    $found = true;
                    continue;
                }

                if ($found !== true || $key >= $baseCount) {
                    continue;
                }

                $tmp              = $nextLevelElement;
                $newCombination   = array_slice($tmp, 0);
                $newCombination[] = $element;
                $result[]         = array_slice($newCombination, 0);
            }
        }

        return $result;
    }
private function getPossibleCombinations(array $elements, $combinationsLevel)
    {
        $baseCount = count($elements);
        $result    = [];

        if ($combinationsLevel === 1) {
            foreach ($elements as $element) {
                $result[] = [$element];
            }

            return $result;
        }

        $nextLevelElements = $this->getPossibleCombinations($elements, $combinationsLevel - 1);

        foreach ($nextLevelElements as $nextLevelElement) {
            $lastElement = $nextLevelElement[$combinationsLevel - 2];
            $found       = false;

            foreach ($elements as $key => $element) {
                if ($element === $lastElement) {
                    $found = true;
                    continue;
                }

                if ($found !== true || $key >= $baseCount) {
                    continue;
                }

                $tmp              = $nextLevelElement;
                $newCombination   = array_slice($tmp, 0);
                $newCombination[] = $element;
                $result[]         = array_slice($newCombination, 0);
            }
        }

        return $result;
    }
private function getPossibleCombinations ( array $ elements , $ combinationsLevel ) { $ baseCount = count ( $ elements ) ; $ result = [ ] ; if ( $ combinationsLevel === 1 ) { foreach ( $ elements as $ element ) { $ result [ ] = [ $ element ] ; } return $ result ; } $ nextLevelElements = $ this -> getPossibleCombinations ( $ elements , $ combinationsLevel - 1 ) ; foreach ( $ nextLevelElements as $ nextLevelElement ) { $ lastElement = $ nextLevelElement [ $ combinationsLevel - 2 ] ; $ found = false ; foreach ( $ elements as $ key => $ element ) { if ( $ element === $ lastElement ) { $ found = true ; continue ; } if ( $ found !== true || $ key >= $ baseCount ) { continue ; } $ tmp = $ nextLevelElement ; $ newCombination = array_slice ( $ tmp , 0 ) ; $ newCombination [ ] = $ element ; $ result [ ] = array_slice ( $ newCombination , 0 ) ; } } return $ result ; }
Get the possible combinations of elements from the given array

@param array $elements
@param int   $combinationsLevel

@return array
Get the possible combinations of elements from the given array