Look into permutations. O'Reilley has some good information on this via google. If I have some extra time, I will try and draft up an example for you. 
Update
Here is some code, not 100% if it works correctly, but you should be able to modify it to your needs (the core code came from the O'Reilley site, fyi):
<?php
function pc_permute($items, $perms = array( )) {
    if (empty($items)) { 
        print 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);
             pc_permute($newitems, $newperms);
         }
    }
}
pc_permute(array('abc', 'xyz', 'def', 'hij'));
?>
EDIT
Just saw that he wanted the algorithm, either or the code should produce the results for other lurkers :) See other answers for the algorithm, which is n!