title: Match values of two different arrays & Rebuild
body: <p>I need to match the values of two different arrays. If <code>position</code> from <code>array_1</code> is in <code>array_2</code> then assign the position &amp; description to a new array. If the <code>position</code> from <code>array_1</code> is not in <code>array_2</code> then assign the <code>position</code> with an empty description to the new array.</p>
  
  <p>My PHP code (below) isn't working because of the second <code>foreach</code>. It's overwriting the new array (<code>position_array</code>). It's only writing the last description.</p>
  
  <p>array_1:</p>
  
  <pre><code>Array
  (
      [0] =&gt; 1
      [1] =&gt; 2
      [2] =&gt; 3
      [3] =&gt; 4
      [4] =&gt; 6
      [5] =&gt; 12
  )
  </code></pre>
  
  <p>array_2:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [position] =&gt; 2
              [description] =&gt; Text 1
          )
  
      [1] =&gt; Array
          (
              [position] =&gt; 4
              [description] =&gt; Text 2
          )
  
      [2] =&gt; Array
          (
              [position] =&gt; 6
              [description] =&gt; Text 3
          )
  
      [3] =&gt; Array
          (
              [position] =&gt; 12
              [description] =&gt; Text 4
          )
  )
  </code></pre>
  
  <p>PHP:</p>
  
  <pre><code>print_r($_POST['position']);
  print_r($_POST['position_desc']);
  
  $position=$_POST['position'];
  # Check if there was POST data sent.
  if(isset($_POST['position_desc']))
  {
      # Create an empty array.
      $position_array=array();
      # Loop through the user's positions.
      foreach($position as $position_key=&gt;$position_value)
      {
          foreach($_POST['position_desc'] as $desc_key=&gt;$position_desc)
          {
              echo $position_value.' -&gt; '.$position_desc['position'].' - ';
              if($position_value==$position_desc['position'])
              {
                  echo 'MATCH - Assign Desc&lt;br&gt;';
                  # Assign the position and description to the array.
                  $position_array[$position_key]=array('position'=&gt;$position_value, 'description'=&gt;$position_desc['description']);
              }
              else
              {
                  echo 'NO MATCH - Assign empty Desc&lt;br&gt;';
                  # Assign the position and description to the array.
                  $position_array[$position_key]=array('position'=&gt;$position_value, 'description'=&gt;'');
              }
          }
      }
      print_r($position_array);
      exit;
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>1 -&gt; 2 - NO MATCH - Assign empty Desc
  1 -&gt; 4 - NO MATCH - Assign empty Desc
  1 -&gt; 6 - NO MATCH - Assign empty Desc
  1 -&gt; 12 - NO MATCH - Assign empty Desc
  2 -&gt; 2 - MATCH - Assign Desc
  2 -&gt; 4 - NO MATCH - Assign empty Desc
  2 -&gt; 6 - NO MATCH - Assign empty Desc
  2 -&gt; 12 - NO MATCH - Assign empty Desc
  3 -&gt; 2 - NO MATCH - Assign empty Desc
  3 -&gt; 4 - NO MATCH - Assign empty Desc
  3 -&gt; 6 - NO MATCH - Assign empty Desc
  3 -&gt; 12 - NO MATCH - Assign empty Desc
  4 -&gt; 2 - NO MATCH - Assign empty Desc
  4 -&gt; 4 - MATCH - Assign Desc
  4 -&gt; 6 - NO MATCH - Assign empty Desc
  4 -&gt; 12 - NO MATCH - Assign empty Desc
  6 -&gt; 2 - NO MATCH - Assign empty Desc
  6 -&gt; 4 - NO MATCH - Assign empty Desc
  6 -&gt; 6 - MATCH - Assign Desc
  6 -&gt; 12 - NO MATCH - Assign empty Desc
  12 -&gt; 2 - NO MATCH - Assign empty Desc
  12 -&gt; 4 - NO MATCH - Assign empty Desc
  12 -&gt; 6 - NO MATCH - Assign empty Desc
  12 -&gt; 12 - MATCH - Assign Desc
  
  Array
  (
      [0] =&gt; Array
          (
              [position] =&gt; 1
              [description] =&gt; 
          )
  
      [1] =&gt; Array
          (
              [position] =&gt; 2
              [description] =&gt; 
          )
  
      [2] =&gt; Array
          (
              [position] =&gt; 3
              [description] =&gt; 
          )
  
      [3] =&gt; Array
          (
              [position] =&gt; 4
              [description] =&gt; 
          )
  
      [4] =&gt; Array
          (
              [position] =&gt; 6
              [description] =&gt; 
          )
  
      [5] =&gt; Array
          (
              [position] =&gt; 12
              [description] =&gt; Text 4
          )
  
  )
  </code></pre>