Skip to content

Instantly share code, notes, and snippets.

@mikeflynn
Created March 20, 2012 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeflynn/2131717 to your computer and use it in GitHub Desktop.
Save mikeflynn/2131717 to your computer and use it in GitHub Desktop.
given a list of words, i need every unique combination where the list is in alphabetical order.
<?php
function order_list($words, &$return = null)
{
if(!$return) $return = array();
sort($words);
$string = implode(', ', $words);
if(!empty($string) && !in_array($string, $return)) $return[] = $string;
foreach($words as $key=>$word)
{
$temp = $words;
unset($temp[$key]);
sort($temp);
$string = implode(', ', $temp);
if(!empty($string) && !in_array($string, $return)) $return[] = $string;
if(sizeof($temp) > 0) order_list($temp, $return);
}
}
order_list(array('apple', 'zebra', 'cat', 'bear'), $result);
if(!empty($result) && is_array($result))
{
echo "--------\n";
echo implode("\n", $result);
echo "\n--------\n";
}
echo "\nDone.\n";
?>
@mikeflynn
Copy link
Author

Moved this from a private gist to a public.

Another solution: https://gist.github.com/764121

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment