Gehe zu deutscher Webseite

ViaThinkSoft CodeLib

This article is in:
CodeLib → Programming aids → PHP

<?php

$key 
= 'ABCD';

function 
allSwapCombinations($key, $praefix='') {
    
$out = array();
    for (
$i=0; $i<strlen($key); $i++) {
                
$char = $key[$i];
        if (
strlen($key) == 1) {
            
$out[] = $praefix.$char;
        } else {
            
$key2 = substr($key,0,max(0,$i)).substr($key,$i+1);
            
$out = array_merge($out, allSwapCombinations($key2, $praefix.$char));
        }
    }
    return 
$out;
}

print_r(allSwapCombinations($key));
Daniel Marschall
ViaThinkSoft Co-Founder