Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts

    Word Shuffle Software

    Hey, Is their any software or will this be easy to make with php?

    What I want:
    I have to list of words, for this example lets says "buy words" and "Food" words.

    Buy words: Buy, Purchase
    Food Words: Food, Grub

    Now I need a program where I can enter each separate list and It gives me all the combinations like such:

    Buy Food, Buy Grub, Purchase Food, Purchase Grub.

  • #2
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts
    Here's the function that you want in javascript. In the next few minutes I'll convert it to php, since you mentioned that, and post that as well.
    Code:
    var list1=new Array('wordA','wordB','wordC','wordD');
    var list2=new Array('word1','word2','word3');
    var combos="";
    
    function combinations(array1, array2)
    {
    for(x=0; x<array1.length; x++)
    {
    for(y=0; y<array2.length; y++)
    {
    combos+=array1[x]+' '+array2[y]+', ';
    }
    }
    
    for(a=0; a<array2.length; a++)
    {
    for(b=0; b<array1.length; b++)
    {
    combos+=array2[a]+' '+array1[b]+', ';
    }
    }
    return combos;
    }
    Edit:
    PHP Code:
    $list1=array('wordA','wordB','wordC','wordD');
    $list2=array('word1','word2','word3');
    $combos="";

    function 
    combinations($array1$array2)
    {
    $array1len=count($array1);
    $array2len=count($array2);
    for(
    $x=0$x<$array1len$x++)
    {
    for(
    $y=0$y<$array2len$y++)
    {
    $combos.=$array1[$x].' '.$array2[$y].', ';
    }
    }

    for(
    $a=0$a<$array1len$a++)
    {
    for(
    $b=0$b<$array2len$b++)
    {
    $combos.=$array1[$a].' '.$array2[$b].', ';
    }
    }
    return 
    $combos;

    I also updated the JS version to use a return.
    Last edited by binaryWeapon; 03-16-2008 at 12:37 AM. Reason: Finished php version and updated JS

  • Users who have thanked binaryWeapon for this post:

    srule_ (03-16-2008)

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts
    Hey, Thanks alot! Only problem is it outputs results twice. using my example above it out puts "Buy Food, Buy Grub, Purchase Food, Purchase Grub, Buy Food, Buy Grub, Purchase Food, Purchase Grub"

    instead of just "Buy Food, Buy Grub, Purchase Food, Purchase Grub"

  • #4
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    You could store each result in an array and check each time you get a new result to see if it is in the array. If it is, throw it away.

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    binaryWeapon code:

    Code:
    var list1=new Array('wordA','wordB','wordC','wordD');
    var list2=new Array('word1','word2','word3');
    
    function combinations(array1, array2){
       var combos="";
       for(x=0; x<array1.length; x++){
         for(y=0; y<array2.length; y++){
            combos+=array1[x]+' '+array2[y]+', ';
         }
       }
       return combos;
    }
    PHP Code:
    $list1=array('wordA','wordB','wordC','wordD');
    $list2=array('word1','word2','word3');

    function 
    combinations($array1$array2){
      
    $combos="";
      
    $array1len=count($array1);
      
    $array2len=count($array2);
      for(
    $x=0$x<$array1len$x++){
         for(
    $y=0$y<$array2len$y++){
            
    $combos.=$array1[$x].' '.$array2[$y].', ';
         }
      }
      return 
    $combos;

    best regards

  • #6
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts
    The way I coded it, the function would show every possible combination, so the code
    Code:
    var list1=new Array('buy','purchase');
    var list2=new Array('food','grub');
    var combos="";
    
    function combinations(array1, array2)
    {
    for(x=0; x<array1.length; x++)
    {
    for(y=0; y<array2.length; y++)
    {
    combos+=array1[x]+' '+array2[y]+', ';
    }
    }
    
    for(a=0; a<array2.length; a++)
    {
    for(b=0; b<array1.length; b++)
    {
    combos+=array2[a]+' '+array1[b]+', ';
    }
    }
    return combos;
    }
    
    alert(combinations(list1,list2));
    Would alert "Buy food, buy grub, purchase food, purchase grub,..." and then continue with the words food and grub first, so the same two words would reappear, but in a different order... "food buy, food purchase, grub buy, grub purchase". If you just need one set of words at the beginnings of each string of words, use oesxyl's, which works perfectly.

    Cheers!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •