0

I have one array with names(John, Suzy, Mary, June, ...)one with domains (@hotmail, @gmail, @yahoo, @tv, ...)and another one with email extensions(.com, .net, .co.uk...).

I have all these stored in db each in a different table. Im using a loop to get the values from db.

My problem is that Im using 3 loops in order to try the different possibilities for email. It is too slow and the page freezes. I know there must be a more practical and elegant way like using one of the inbuilt array functions. example "array_map"? Any help please guys.

  foreach ($firstNames->data as $fn){

      foreach ($domains->data as $d){

             foreach ($emailExtensions->data as $de){

                    //add numbers to emails
                   for($n=0;$n<20;$n++){

                       $emails.= $fn['name'].$n.$d['domain'].$de['extension']." <br>";

                         //add dash before number
                         $emails.= $fn['name']."-".$n.$d['domain'].$de['extension']." <br>";


                      }

             $emails.= $fn['name'].$d['domain'].$de['extension']." ".$is_valid." <br>";   


         }

     }

 }

      echo  "<h4> " . $emails. "</h4><br>";
Mike Spider
  • 217
  • 1
  • 3
  • 17
  • The truly practical/elegant way is to leverage your properly set up relational database to merge these three tables using a purpose built JOINing query and avoid the task of forcing php to have to chew on your 3 resultsets. If you think this is not accurate, you'll need to put forth a rather compelling argument as to why. – mickmackusa Mar 15 '17 at 08:08
  • 1
    Let me ask - what is the purpose of such kind of "random" email list? It seems like building of spamlist to me... – Jan Rydrych Mar 15 '17 at 09:32
  • @mickmackusa. I will have to try it out. I confess joining tables and using MYSQLI own functions did cross my mind as a solution. But I'm new with PHP and MYSQL. I know it will be considered as a new question , but can you point ne in the right direction please? Thanks for your reply. – Mike Spider Mar 15 '17 at 13:16
  • @Honza. Yes more or less this is a school assignment we are testing a spam detector in one email box we built in order to automate it so the spam originator is sent to junk folder and blocked. – Mike Spider Mar 15 '17 at 13:23
  • without seeing your database structure, I can't offer any accurate advice. Just start researching the many different JOINs sql has to offer. Go directly into your database and start experimenting with queries and see what results you can SELECT. Fear not, we were all new once. – mickmackusa Mar 15 '17 at 13:24
  • Maybe this qestion helps - http://stackoverflow.com/questions/6311779/finding-cartesian-product-with-php-associative-arrays – Roman Hocke Mar 16 '17 at 12:26

2 Answers2

0
   $names = array('abbasalibutt', 'bilal');
$domains = array('@gmail','@yahoo');
$extensions = array('.com', '.ca');

$emailsParts = array();
$emails = array();

foreach($domains as $domain){
    foreach($extensions as $extension){
        $emailsParts[] =  $domain.$extension;
    }
}


foreach($names as $name){
    foreach($emailsParts as $emailsPart){
        $emails[] =  $name.$emailsPart;
    }
}

foreach ($emails as $email){
    echo $email.'<br/>';
}
Abbas
  • 763
  • 1
  • 12
  • 26
  • 1
    It is how we can achieve your requirement with two loops. And the other best way is to use the Left join etc based quries. I hope it will help for you. – Abbas Mar 15 '17 at 08:19
0

The problem was solved by MYSQL CONCAT() function:

    $sql= "SELECT CONCAT(name, domain, extension) as emails FROM 
           first_names, emails_domains, emails_extensions";

Then I just use one PHP "while" loop to get the results. Easy, amazingly quick. Beautiful. :) I'm most certainly will start thinking? Mysqli first, php second. Thanks guys for all the help, motivation and pointing me in the right direction.

Mike Spider
  • 217
  • 1
  • 3
  • 17