title: Getting $media all the time with values
body: <p>I have this code for move inside arrays and build a proper JSON response but something is not working since <code>$media</code> got always the same values and shouldn't be cause in the 2nd iteration there are not <code>$media</code>. What is wrong in my logic? Any advice?</p>
  
  <p><strong>Code snippet</strong></p>
  
  <pre><code>for ($i = 0; $i &lt; count($entEmail); $i++) {
      $entMedia = $em-&gt;getRepository('AppBundle:Representative')-&gt;getAllEmailsMedia($entEmail[$i]['id']);
  
      for ($j = 0; $j &lt; count($entMedia); $j++) {
          $media[] = [
              "media_id" =&gt; $entMedia[$j]['id'],
              "title" =&gt; $entMedia[$j]['title'],
              "description" =&gt; $entMedia[$j]['description'],
              "thumbnail_url" =&gt; $entMedia[$j]['thumbnail_url'],
              "viewedAt" =&gt; $em-&gt;getRepository('AppBundle:Representative')-&gt;getMediaViewedAt($entEmail[$i]['id'], $entMedia[$j]['id']) ? $em-&gt;getRepository('AppBundle:Representative')-&gt;getMediaViewedAt($entEmail[$i]['id'], $entMedia[$j]['id']) : "0000-00-00 00:00:00"
          ];
      }
  
      $response[] = [
          "id" =&gt; $entEmail[$i]['id'],
          "rep_id" =&gt; $entEmail[$i]['reps_id'],
          "target_id" =&gt; $entEmail[$i]['targets_id'],
          "brands_id" =&gt; $entEmail[$i]['brands_id'],
          "brand_name" =&gt; $entEmail[$i]['name'],
          "messages_id" =&gt; $entEmail[$i]['messages_id'],
          "message_text" =&gt; $entEmail[$i]['message_text'],
          "media" =&gt; $media,
          "category" =&gt; $entEmail[$i]['category'],
          "createdAt" =&gt; $entEmail[$i]['createdAt'],
          "openedAt" =&gt; $em-&gt;getRepository('AppBundle:Representative')-&gt;getEmailOpenetAt($entEmail[$i]['id']) ? $em-&gt;getRepository('AppBundle:Representative')-&gt;getEmailOpenetAt($entEmail[$i]['id']) : "0000-00-00 00:00:00",
          "viewedAt" =&gt; $em-&gt;getRepository('AppBundle:Representative')-&gt;getEmailViewedAt($entEmail[$i]['id']) ? $em-&gt;getRepository('AppBundle:Representative')-&gt;getEmailViewedAt($entEmail[$i]['id']) : "0000-00-00 00:00:00"
      ];
  }
  </code></pre>
  
  <p><strong>Current Response</strong></p>
  
  <pre><code>[
    {
      "id": "1",
      "rep_id": "1",
      "target_id": "1",
      "brands_id": "1",
      "brand_name": "Combivent Respimat",
      "messages_id": "1",
      "message_text": "Dear [HCP Name],  Thank you for taking the time to speak with me today.  Based on our discussion, I’ve attached additional materials about COMBIVENT RESPIMAT.  I look forward to speaking with you again in the near future.",
      "media": [
        {
          "media_id": "1",
          "title": "Abbreviated Visual Aid",
          "description": "Provides information on key features of COMBIVENT RESPIMAT, including safety and efficacy information, the RESPIMAT Inhaler.",
          "thumbnail_url": "https://pdone.s3.amazonaws.com/thumbnail/abbreviated_vis_aid.png",
          "viewedAt": "2015-05-24 23:27:41"
        },
        {
          "media_id": "2",
          "title": "Solutions Plus™ HCP Flashcard",
          "description": "Information about the Solutions Plus phone line that offers support services and education to both patients and HCPs.",
          "thumbnail_url": "https://pdone.s3.amazonaws.com/thumbnail/sp_flash_card.png",
          "viewedAt": "2015-05-24 23:27:42"
        },
        {
          "media_id": "3",
          "title": "Solutions Plus™ Patient Flashcard",
          "description": "A detailed list of the services Solutions Plus offers to patients that you can print and give to patients together with a prescription for COMBIVENT RESPIMAT.",
          "thumbnail_url": "https://pdone.s3.amazonaws.com/thumbnail/sp_patient_card.png",
          "viewedAt": "0000-00-00 00:00:00"
        },
        {
          "media_id": "4",
          "title": "Abbreviated Instructions for Use",
          "description": "A step-by-step patient guide listing the steps for COMBIVENT RESPIMAT preparation, priming, and dosing, with information on safety and important features of COMBIVENT RESPIMAT.",
          "thumbnail_url": "https://pdone.s3.amazonaws.com/thumbnail/patient_tearsheet.png",
          "viewedAt": "0000-00-00 00:00:00"
        },
        {
          "media_id": "5",
          "title": "Patient Brochure",
          "description": "A patient-friendly starter guide to the safety, benefits, and use of COMBIVENT RESPIMAT.",
          "thumbnail_url": "https://pdone.s3.amazonaws.com/thumbnail/patient_brochure.png",
          "viewedAt": "0000-00-00 00:00:00"
        },
        {
          "media_id": "6",
          "title": "RESPIMAT Inhaler Training Video",
          "description": "A step-by-step instructional video that details how to prepare, prime, and use the RESPIMAT Inhaler for daily dosing.",
          "thumbnail_url": "https://pdone.s3.amazonaws.com/thumbnail/ihi_video.png",
          "viewedAt": "0000-00-00 00:00:00"
        }
      ],
      "category": "REP",
      "createdAt": "2015-05-24 12:00:00",
      "openedAt": "2015-05-24 23:27:40",
      "viewedAt": "2015-05-24 22:54:08"
    },
    {
      "id": "2",
      "rep_id": "1",
      "target_id": "1",
      "brands_id": "1",
      "brand_name": "Combivent Respimat",
      "messages_id": "5",
      "message_text": "I would like to schedule a live representative visit.",
      "media": [
        {
          "media_id": "1",
          "title": "Abbreviated Visual Aid",
          "description": "Provides information on key features of COMBIVENT RESPIMAT, including safety and efficacy information, the RESPIMAT Inhaler.",
          "thumbnail_url": "https://pdone.s3.amazonaws.com/thumbnail/abbreviated_vis_aid.png",
          "viewedAt": "2015-05-24 23:27:41"
        },
        {
          "media_id": "2",
          "title": "Solutions Plus™ HCP Flashcard",
          "description": "Information about the Solutions Plus phone line that offers support services and education to both patients and HCPs.",
          "thumbnail_url": "https://pdone.s3.amazonaws.com/thumbnail/sp_flash_card.png",
          "viewedAt": "2015-05-24 23:27:42"
        },
        {
          "media_id": "3",
          "title": "Solutions Plus™ Patient Flashcard",
          "description": "A detailed list of the services Solutions Plus offers to patients that you can print and give to patients together with a prescription for COMBIVENT RESPIMAT.",
          "thumbnail_url": "https://pdone.s3.amazonaws.com/thumbnail/sp_patient_card.png",
          "viewedAt": "0000-00-00 00:00:00"
        },
        {
          "media_id": "4",
          "title": "Abbreviated Instructions for Use",
          "description": "A step-by-step patient guide listing the steps for COMBIVENT RESPIMAT preparation, priming, and dosing, with information on safety and important features of COMBIVENT RESPIMAT.",
          "thumbnail_url": "https://pdone.s3.amazonaws.com/thumbnail/patient_tearsheet.png",
          "viewedAt": "0000-00-00 00:00:00"
        },
        {
          "media_id": "5",
          "title": "Patient Brochure",
          "description": "A patient-friendly starter guide to the safety, benefits, and use of COMBIVENT RESPIMAT.",
          "thumbnail_url": "https://pdone.s3.amazonaws.com/thumbnail/patient_brochure.png",
          "viewedAt": "0000-00-00 00:00:00"
        },
        {
          "media_id": "6",
          "title": "RESPIMAT Inhaler Training Video",
          "description": "A step-by-step instructional video that details how to prepare, prime, and use the RESPIMAT Inhaler for daily dosing.",
          "thumbnail_url": "https://pdone.s3.amazonaws.com/thumbnail/ihi_video.png",
          "viewedAt": "0000-00-00 00:00:00"
        }
      ],
      "category": "TARGET",
      "createdAt": "2015-05-24 23:27:40",
      "openedAt": "0000-00-00 00:00:00",
      "viewedAt": "0000-00-00 00:00:00"
    }
  ]
  </code></pre>
  
  <p><strong>Expected Response</strong></p>
  
  <pre><code>[
    {
      "id": "1",
      "rep_id": "1",
      "target_id": "1",
      "brands_id": "1",
      "brand_name": "Combivent Respimat",
      "messages_id": "1",
      "message_text": "Dear [HCP Name],  Thank you for taking the time to speak with me today.  Based on our discussion, I’ve attached additional materials about COMBIVENT RESPIMAT.  I look forward to speaking with you again in the near future.",
      "media": [
        {
          "media_id": "1",
          "title": "Abbreviated Visual Aid",
          "description": "Provides information on key features of COMBIVENT RESPIMAT, including safety and efficacy information, the RESPIMAT Inhaler.",
          "thumbnail_url": "https://pdone.s3.amazonaws.com/thumbnail/abbreviated_vis_aid.png",
          "viewedAt": "2015-05-24 23:27:41"
        },
        {
          "media_id": "2",
          "title": "Solutions Plus™ HCP Flashcard",
          "description": "Information about the Solutions Plus phone line that offers support services and education to both patients and HCPs.",
          "thumbnail_url": "https://pdone.s3.amazonaws.com/thumbnail/sp_flash_card.png",
          "viewedAt": "2015-05-24 23:27:42"
        },
        {
          "media_id": "3",
          "title": "Solutions Plus™ Patient Flashcard",
          "description": "A detailed list of the services Solutions Plus offers to patients that you can print and give to patients together with a prescription for COMBIVENT RESPIMAT.",
          "thumbnail_url": "https://pdone.s3.amazonaws.com/thumbnail/sp_patient_card.png",
          "viewedAt": "0000-00-00 00:00:00"
        },
        {
          "media_id": "4",
          "title": "Abbreviated Instructions for Use",
          "description": "A step-by-step patient guide listing the steps for COMBIVENT RESPIMAT preparation, priming, and dosing, with information on safety and important features of COMBIVENT RESPIMAT.",
          "thumbnail_url": "https://pdone.s3.amazonaws.com/thumbnail/patient_tearsheet.png",
          "viewedAt": "0000-00-00 00:00:00"
        },
        {
          "media_id": "5",
          "title": "Patient Brochure",
          "description": "A patient-friendly starter guide to the safety, benefits, and use of COMBIVENT RESPIMAT.",
          "thumbnail_url": "https://pdone.s3.amazonaws.com/thumbnail/patient_brochure.png",
          "viewedAt": "0000-00-00 00:00:00"
        },
        {
          "media_id": "6",
          "title": "RESPIMAT Inhaler Training Video",
          "description": "A step-by-step instructional video that details how to prepare, prime, and use the RESPIMAT Inhaler for daily dosing.",
          "thumbnail_url": "https://pdone.s3.amazonaws.com/thumbnail/ihi_video.png",
          "viewedAt": "0000-00-00 00:00:00"
        }
      ],
      "category": "REP",
      "createdAt": "2015-05-24 12:00:00",
      "openedAt": "2015-05-24 23:27:40",
      "viewedAt": "2015-05-24 22:54:08"
    },
    {
      "id": "2",
      "rep_id": "1",
      "target_id": "1",
      "brands_id": "1",
      "brand_name": "Combivent Respimat",
      "messages_id": "5",
      "message_text": "I would like to schedule a live representative visit.",
      "media": [],
      "category": "TARGET",
      "createdAt": "2015-05-24 23:27:40",
      "openedAt": "0000-00-00 00:00:00",
      "viewedAt": "0000-00-00 00:00:00"
    }
  ]
  </code></pre>