PHP: Return all String Permutations for a 1 Dimensional Array CodeUnit 07 FEB 2011

Getting all the possible string permutations of an array can be pretty useful if you are trying to unlock something and this nifty little function does exactly that:

function array_permutations($items, $perms = array()) {
    static $permuted_array;
    if (empty($items)) {
        $permuted_array[] = $perms;
    } else {
        for ($i = count($items) - 1; $i >= 0; --$i) {
            $newitems = $items;
            $newperms = $perms;
            list($foo) = array_splice($newitems, $i, 1);
            array_unshift($newperms, $foo);
            array_permutations($newitems, $newperms);
        return $permuted_array;

//To use:


About Craig Lotter

Software developer, husband and dad to two young ladies. Writer behind An Exploring South African. I don't have time for myself any more.