Can we mix them?

Discussion in 'PHP' started by semitrico, Jan 14, 2009.

  1. #1
    There are 2 list first a list containing names of females e.g. (sara, amanda,anna etc..)
    also a list containing names of males ( nolan, chris,steven etc...)

    I want to use php to make all possible mixtures of 2 names like (sara nolan, sara steven,amanda chris, anna steven etc...) but putting in mind that the first name has to be a female and the second a male, is it possible?
     
    semitrico, Jan 14, 2009 IP
  2. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #2
    I've made this quick code for you. It takes a list from two files (every name should be on a new line), and makes all names from that.

    
    <?
    $femaleNames = "femalenames.txt";
    $maleNames = "malenames.txt";
    $fn = file_get_contents($femaleNames);
    $fn = explode("\n",$fn);
    $mn = file_get_contents($maleNames);
    $mn = explode("\n",$mn);
    for($i = 0;$i <= count($fn);$i++){
    	for($x = 0;$x <= count($mn);$x++){
    		if($fn[$i] != "" && $mn[$x] != ""){
    			echo $fn[$i] . " " . $mn[$x] . "<br />";
    		}
    	}
    }
    ?>
    
    PHP:
     
    elias_sorensen, Jan 14, 2009 IP
  3. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If I understand correctly - this is a way to do what you want - you have two arrays - $female and $male, which contain the names. To mix them you can do the fallowing:
    foreach($female AS $first_name)
    {
       $names[] = $first_name." ".$male[rand(0, count($male))];
    }
    Code (markup):
    This will get all the names from $female, add a random name from $male and save the full names in the array $names. Hope that helps :)
     
    xlcho, Jan 14, 2009 IP
  4. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #4
    But I think he want ALL name-combinations to make. Not just a random male-name.
     
    elias_sorensen, Jan 14, 2009 IP
  5. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    OK then, it's even easier :)
    foreach($female AS $key => $first_name)
    {
       $names[] = $first_name." ".$male[$key];
    }
    Code (markup):
    But this code will only work correctly if both arrays ($male and $female) have the same number of elements. It will also come up with the same result each time, which is kinda dumb and useless IMHO (it depends on what you wanna do but most of the time at least..) :)
     
    xlcho, Jan 15, 2009 IP
  6. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #6
    xlcho: If you have this list:
    Anna
    Sara
    -----------------
    James
    John
    Steven
    Chris

    It wouln't make all combinations of those?
     
    elias_sorensen, Jan 15, 2009 IP
  7. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I didn't read the task well, sorry. It's a pretty simple "5 lines of code" script though. If you can't write this one on your own and you are trying to build something big, just quit it till you learn some more programming first. Here's it:
    foreach($female AS $first_name)
    {
       foreach($male AS $last_name)
       {
          $names[] = $first_name." ".$last_name;
       }
    }
    Code (markup):
     
    xlcho, Jan 15, 2009 IP
  8. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #8
    Hey hey, this isn't my thread.. I know how to do it - I just pointed out that the code didn't do what he wanted :)
     
    elias_sorensen, Jan 15, 2009 IP
  9. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #9
    This was just a recommendation to semitrico and all the others who wanna build a space ship before they learn how to build a raft :)
     
    xlcho, Jan 15, 2009 IP