Just Sharing Knowledge on Robotics, Electronics and Programming

Tuesday, June 5, 2018

[SOLVED] PHP Permutation of given words with all posibilities of combination



If you want to create a permutation with all posibilities of words in PHP, you can use the following script

if(isset($_POST['submit'])){
    $data      = $_POST['data'];
    $arr = explode(' ',$data);
    permutasikan($arr);
}
function permutasikan($items, $perms = array()) {
    if (empty($items)) {
        echo join(' ', $perms) . "\n";
    } else {
        for ($i = count($items) - 1; $i >= 0; --$i) {
             $newitems = $items;
             $newperms = $perms;
             list($foo) = array_splice($newitems, $i, 1);
             array_unshift($newperms, $foo);
             permutasikan($newitems, $newperms);
         }
    }
}

0 comments:

Post a Comment