Migrate data between tables efficiently I wrote a very simple quick and easy script to migrate a bunch of data to another table in my database.  

Can I accomplish this task without using so many foreach loops?
I realize I'm imputting similar data in each of my loops and it just seems redundant and wrong. 

I thought of using array_merge on talkers, pros &amp; anons and I thought that   because each of the second loops use the same field, I would write code to retrieve getYI and then write code to determine if it belonged to a talker, anon, or pro.
However I wasn't sure if either of those solutions would make my script any more efficient/better.

Certainly any insight would be appreciated. Thanks in advance!

And if it helps I'm using Symfony 2.3. 

public function migratingDataScript()
{
    $em = $this-&gt;getDoctrine()-&gt;getManager();

    $talkers = $em-&gt;getRepository('AppBundle:Talker')-&gt;findAll();
    $anons = $em-&gt;getRepository('AppBundle:Anon')-&gt;findAll();
    $pros = $em-&gt;getRepository('AppBundle:Pro')-&gt;findAll();

    foreach ($talkers as $talker) {
        echo "Talker: ".$talker-&gt;getId()."&lt;br&gt;";
        foreach ($talker-&gt;getYI() as $year =&gt; $status) {
            $rounds = $em-&gt;getRepository('AppBundle:Round')-&gt;findByYear($year);
            foreach ($rounds as $round) {
                $newTalkerInvitation = new Invitation($round);
                $newTalkerInvitation-&gt;setStatus($status);
                $newTalkerInvitation-&gt;setTalker($talker);
                // $newTalkerInvitation-&gt;setNumAt(); No Data For Talker
                // $newTalkerInvitation-&gt;setStart(); No Data For Talker
                $em-&gt;persist($newTalkerInvitation);
            }
        }
    }//Ends Talker Migration

    foreach ($anons as $anon) {
        echo "Anon: ".$anon-&gt;getId()."&lt;br&gt;";
        foreach ($anon-&gt;getYI() as $year =&gt; $status) {
            $rounds = $em-&gt;getRepository('AppBundle:Round')-&gt;findByYear($year);
            foreach ($rounds as $round) {
                $newAnonInvitation = new Invitation($round);
                $newAnonInvitation-&gt;setStatus($status);
                $newAnonInvitation-&gt;setAnon($anon);
                // $newAnonInvitation-&gt;setNumAt(); No Data For Anon
                // $newAnonInvitation-&gt;setStart(); No Data For Anon
                $em-&gt;persist($newAnonInvitation);
            }
        }
    }//Ends Anon Migration

    foreach ($pros as $pro) {
        echo "Pro: ".$pro-&gt;getId()."&lt;br&gt;";
        foreach ($pro-&gt;getYI() as $year =&gt; $status) {
            $rounds = $em-&gt;getRepository('AppBundle:Round')-&gt;findByYear($year);
            foreach ($rounds as $round) {
                $newProInvitation = new Invitation($round);
                $newProInvitation-&gt;setStatus($status);
                $newProInvitation-&gt;setPro($pro);
                if(is_null($pro-&gt;getNumOfAttempts())){
                    $newProInvitation-&gt;setNumAt('0');
                }else {
                    $newProInvitation-&gt;setNumAt($pro-&gt;getNumOfAttempts());
                }
                // $newProInvitation-&gt;setStart(); No Data For Pro
                $em-&gt;persist($newProInvitation);
            }
        }
    }//Ends Pro Migration

    $em-&gt;flush();
    echo "Completed.";
    die;
}
Can I accomplish this task without using so many foreach loops?


Depends.

If you want to stick with database-agnostic abstraction such as Doctrine, there is no way to avoid loops.

But if you want an efficient solution, you can write a database-specific query to copy all records in a single query. For example, for mysql it would be something like 

INSERT INTO invitation (field1, field2, status, talker) 
  SELECT field1, field2, ?, ? FROM talker


and then bind $status and $talker to this query.