Displays possible string combinations

I try to take a string and display its possible combinations (in PHP), but speaking in the order of each word. For example: "how do you" return (array)

How are you
How are
are you
how
you
are

The code that I have now displays all the combinations, but I want it to put them in order, rather than turning the words. Anyone have ideas or snippets that they want to share? Thanks

+3
source share
3 answers

Define two iterators and print everything in between. So something like this:

<?
$str = "How are you";
$words = explode(" ",$str);
$num_words = count($words);
for ($i = 0; $i < $num_words; $i++) {
  for ($j = $i; $j < $num_words; $j++) {
    for ($k = $i; $k <= $j; $k++) {
       print $words[$k] . " ";
    }
    print "\n";
  }
}
?>

Output


How 
How are 
How are you 
are 
are you 
you 
+5
source

I know this is a very old post, but the other answer is not very flexible, so I thought I would bring a new answer.


Explanation

, , :

(2 n) - 1

:

(2 3) - 1 = (8) - 1 = 7

, ? , ( , " " ($results = [[]];)), .

Array with the words/numbers (Empty array is '[]'):
[1, 2, 3]

                               //↓new combinations for the next iteration
                               β”‚
iteration 0:

    Combinations:
                  - []         β”‚  -> []
                                  β”‚
iteration 1:        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
                    β”‚             β”‚
    Combinations:   v             v
                  - []    + 1  β”‚  -> [1]    
                                  β”‚
iteration 2:        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
                    β”‚             β”‚
    Combinations:   v             v
                  - []    + 2  β”‚  -> [2]
                  - [1]   + 2  β”‚  -> [1,2]    
                                  β”‚
iteration 3:        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
                    β”‚             β”‚
    Combinations:   v             v
                  - []    + 3  β”‚  -> [3]
                  - [1]   + 3  β”‚  -> [1,3]
                  - [2]   + 3  β”‚  -> [2,3]         
                  - [1,2] + 3  β”‚  -> [1,2,3]    
                               //^ All combinations here

, , : (2^n)-1 . , , array_filter(), array_values(), .

<?php

    $str = "how are you";


    function getCombinations($array) {

        //initalize array
        $results = [[]];

        //get all combinations
        foreach ($array as $k => $element) {
            foreach ($results as $combination)
                $results[] =  $combination + [$k => $element];
        }

        //return filtered array
        return array_values(array_filter($results));

    }


    $arr = getCombinations(explode(" ", $str));

    foreach($arr as $v)
        echo implode(" ", $v) . "<br />";


?>

:

how
are
how are
you
how you
are you
how are you
+2

Answer the question of the combination of a PHP array in reverse order . There it was necessary to get all possible combinations of array elements and save the following.

<?php

$alphabet = array('a','b','c');
$result = array();
$arrResult = array();

// recursively create all possible combinations {
combine($alphabet, $result, $arrResult);

function combine($shiftedAlphabet, &$result, &$arrResult) {
    global $alphabet;

    $currentElementStr = '';
    $currentElementArr = array();
    for($i = 0; $i < count($shiftedAlphabet); ++$i) {
        $newElement = $shiftedAlphabet[$i];
        $currentElementStr .= $newElement;
        $currentElementArr[] = $newElement;

        if(!in_array($currentElementStr, $result)) { // if not duplicated => push it to result
            // find right position {
            $thisCount = count($currentElementArr);
            $indexFrom = 0;
            $indexToInsert = 0;

            // find range of indexes with current count of elements {
            foreach ($arrResult as $arrResultKey => $arrResultValue) {
                $indexToInsert = $arrResultKey + 1;
                if ($thisCount > count($arrResultValue)) {
                    $indexFrom = $indexToInsert;
                }
                if ($thisCount < count($arrResultValue)) {
                    --$indexToInsert;
                    break;
                }
            }
            // find range of indexes with current count of elements }

            // find true index inside true range {
            $trueIndex = $indexToInsert;
            $break = false;
            for($j = $indexFrom; $j < $indexToInsert; ++$j) {
                $trueIndex = $j + 1;
                foreach($arrResult[$j] as $key => $value) {
                    if (array_search($value, $alphabet) > array_search($currentElementArr[$key], $alphabet)) {
                        $break = true;
                        break;
                    }
                }
                if($break) {
                    --$trueIndex;
                    break;
                }
            }
            // find true index inside true range }

            array_splice($result, $trueIndex, 0, $currentElementStr);
            array_splice($arrResult, $trueIndex, 0, array($currentElementArr));
        }
    }

    for($i = 0; $i < count($shiftedAlphabet) - 1; ++$i) {
        $tmpShiftedAlphabet = $shiftedAlphabet; // for combining all possible subcombinations
        array_splice($tmpShiftedAlphabet, $i, 1);
        combine($tmpShiftedAlphabet, $result, $arrResult);
    }
}
// recursively create all possible combinations }
var_dump($result); 

?>

Result here

+1
source

Source: https://habr.com/ru/post/1758434/


All Articles