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?
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:
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
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: If you have this list: Anna Sara ----------------- James John Steven Chris It wouln't make all combinations of those?
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):
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
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