Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make combinations by elements of array using PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 128
    Comment on it

    Hello Reader's If you want to get all elements in the combinations then you can use PHP code to generate it.
    Lets see the example as below:-

    function pc_permute($items, $perms = array()) {
        if (empty($items)) { 
            echo join(' ', $perms) . "<br />";
        } else {
            for ($i = count($items) - 1; $i >= 0; --$i) {
                 $newitems = $items;
                 $newperms = $perms;
                 list($foo) = array_splice($newitems, $i, 1);
                 array_unshift($newperms, $foo);
                 pc_permute($newitems, $newperms);
             }
        }
    }
    

    The array is:-

    $arr = array('peter', 'paul', 'mary');
    
    pc_permute($arr);
    

    Output:-

    peter-paul-mary
    peter-mary-paul
    paul-peter-mary
    paul-mary-peter
    mary-peter-paul
    mary-paul-peter
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: