title: How to prevent php array from merging duplicates
body: <p>To be fair, I'm unsure if an Array Push is the best method for what I'm trying to accomplish, considering it works in Part B of the script.</p>
  <p>I have a ForEach function that creates two (2) arrays with data. The array $ee2[] works as intended (it pulls data from DB, and doesn't duplicate the data). On the contrary, $eRD[] seems to only &quot;append&quot; the data to the array rather than &quot;reserving&quot; the array per iteration.</p>
  <p>Is it better to use something else? I researched other articles and didn't see a fit. I also researched how to use objects instead-but pretty much broke everything. Any suggestions?</p>
  <ol>
  <li>I've tried declaring $eRD[] as an array above the forEach loop - No success, only appends.</li>
  <li>I've tried moving $ee2[] inside of $eRD[]'s forEach loop - it made things wonky.</li>
  <li>I've tried re-structuring the data's output using &quot;separate&quot; variables such as $eRD1, $eRD2.. etc but that seemed to be such a rigorous process.</li>
  <li>I've tried $eRD[$rD-&gt;name] which seems to only grab the &quot;first&quot; instance and uses it globally. $rD-&gt;name is unique to each iteration.</li>
  </ol>
  <p>The $eRD[] forEach finds an associated title and answer for the data it finds.</p>
  <pre><code>$quickQues = new QUICKQUES\Init();
  if(!empty($engs)){
                   foreach($engs as $e){
                       
                       $rawData = json_decode($e['rawSubmittedData']);
                       $rawD = $rawData-&gt;data;
                       
                       foreach($rawD as $rD){
                           
                           $findModule = $quickQues-&gt;thisElementByID($rD-&gt;name);
              
                          if(!empty($findModule)){
                              $moduleTitle = json_decode($findModule['pqfeElement']);
                              $moduleTitleA = $moduleTitle-&gt;qeTitle;
                              $eRD[] = array(
                               'name'=&gt;$moduleTitleA,
                               'value'=&gt;$rD-&gt;value
                               );
                               $moduleTitle = &quot;&quot;;
                               $moduleTitleA = &quot;&quot;;
                          }else{
                              
                          }
                           
                           
                       }
                       
                       $ee2[] = array(
                           'eSPY'=&gt;$e['eSPY'],
                           'submissionElementType'=&gt;$e['submissionElementType'],
                           'submittedBy'=&gt;$e['submittedBy'],
                           'rawSubmittedData'=&gt;$eRD,
                           'isInternalForm'=&gt;$e['isInternalForm'],
                           'eSpyStatus'=&gt;$e['eSpyStatus'],
                           'lastUpdated'=&gt;$e['lastUpdated'],
                           'formName'=&gt;$e['formName']
                           );
                       
                   }
               }
  
  echo json_encode(
          array(  
           array(
              'response'=&gt;'hFetch',
              ),
             array(
              'engagements'=&gt;array(
                  'total'=&gt;$count,
                  'info'=&gt;$ee2,
                  'cEE'=&gt;count($ee2)
                  ),
              ) ) );
  </code></pre>
  <p>After getting help, this method below seems to work perfectly for me. Hope this helps someone else in the future..</p>
  <pre><code>if (!isset($eRD[$rD-&gt;name])) {
                                      $eRD[$rD-&gt;name]['items'] = [];
                                  }
                                  $eRD[$rD-&gt;name]['items'][] = array(
                                           'name'=&gt;$moduleTitleA,
                                           'value'=&gt;$rD-&gt;value
                                  );
  </code></pre>