title: PHP left join failing to join matching records
body: <p>As this solution was adapted from a well-voted answer <a href="https://stackoverflow.com/a/25837426">elsewhere</a>, I was not expecting to encounter problems.</p>
  
  <p>Problem: I want to <code>LEFT JOIN</code> <a href="https://www.dropbox.com/s/gx1oyfj03dsn45p/file0.csv?dl=0" rel="nofollow noreferrer">file0.csv</a> with <a href="https://www.dropbox.com/s/40zndvtmm2azctg/file1.csv?dl=0" rel="nofollow noreferrer">file1.csv</a>.</p>
  
  <p>file0.csv</p>
  
  <pre>
      +-----------------+-------------+--------------+
      | Manufacturer ID |    image    | description  |
      +-----------------+-------------+--------------+
      | SKU231          | image1.jpg  | A box.       |
      | SKUAG1          | image22.jpg | Another box. |
      | SKU21D          | image7a.png | A third box. |
      +-----------------+-------------+--------------+
  </pre>
  
  <p>file1.csv:</p>
  
  <pre>    
      +--------+--------+--------+-------+-------+
      |  mpn   | length | height | width | title |
      +--------+--------+--------+-------+-------+
      | SKU231 |     22 |     14 |    10 | Box 1 |
      | SKUAG1 |     12 |      6 |     6 | Box 2 |
      | SKU21D |      5 |      8 |     5 | Box 3 |
      +--------+--------+--------+-------+-------+
  </pre>    
  
  <p>Desired result (file2.csv):</p>
  
  <pre>    +-----------------+-------------+--------------+--------+--------+--------+-------+-------+
      | Manufacturer ID |    image    | description  |  mpn   | length | height | width | title |
      +-----------------+-------------+--------------+--------+--------+--------+-------+-------+
      | SKU231          | image1.jpg  | A box.       | SKU231 |     22 |     14 |    10 | Box 1 |
      | SKUAG1          | image22.jpg | Another box. | SKUAG1 |     12 |      6 |     6 | Box 2 |
      | SKU21D          | image7a.png | A third box. | SKU21D |      5 |      8 |     5 | Box 3 |
      +-----------------+-------------+--------------+--------+--------+--------+-------+-------+
  </pre>
  
  <p>PHP function to <code>LEFT JOIN</code> <code>file0.csv</code> with <code>file1.csv</code> on <code>Manufacturer ID</code> and <code>mpn</code> respectively:</p>
  
  <pre><code>function my_csv_join(array $csv_input_file_array, $csv_output_file, $html_preview, $left_join_on, $right_join_on = NULL ){
  
  
      if(count($csv_input_file_array) &gt; 2){echo 'This function probably only works for 2 csv files being joined at a time.  Reapply iteratively if needed.  Exiting now.'; exit;}
  
  
  else{
  
      for ($x = 0; $x &lt;= 1; $x++){
      //get csv file to array 
      ${'file'.$x} = fopen($csv_input_file_array[$x], "r"); //Dynamic variables using braces: https://stackoverflow.com/a/9257536/9095603
  
      while (!feof(${'file'.$x})) {
          ${'csv_array'.$x}[] = fgetcsv(${'file'.$x});
      }
  
      fclose(${'file'.$x});
  
      ${'csv_array'.$x} = array_filter(${'csv_array'.$x}); // gets rid of last empty array in case present
  
  
      //CREATE HEADERED ARRAY
  
      ${'header'.$x} = array_shift(${'csv_array'.$x});
  
  
  
      foreach (${'csv_array'.$x} as ${'product_data_array'.$x}) {
  
  
  
      ${'headered_array'.$x}[] = array_combine(${'header'.$x}, ${'product_data_array'.$x});
  
      }        
      }
  
  
  
  // How to simulate the SQL LEFT JOIN operation using PHP arrays?  https://stackoverflow.com/a/25837426
  
  
     //function to simulate the left join
  
  
          $left = $headered_array0;
          $right = $headered_array1;
  
  
          $final= array();
  
          if(empty($right_join_on))// if $right_join_on omitted implies $right_join_on has the same value as $left_join_on
              $right_join_on = $left_join_on;
  
          foreach($left AS $k =&gt; $v){
              $final[$k] = $v; //basically keeping everything; $left just becomes $final
              foreach($right AS $kk =&gt; $vv){
                  if($v[$left_join_on] == $vv[$right_join_on]){   
                      foreach($vv AS $key =&gt; $val)
                          $final[$k][$key] = $val; 
                  } else {
                      foreach($vv AS $key =&gt; $val)
                      $final[$k][$key] = NULL; 
                      }
              }
          }
         if ($html_preview == 'y') {
         echo '&lt;pre&gt;';
         var_dump($final);
         echo '&lt;/pre&gt;';
          }
  
  
      // REINSTATE HEADERS
  
          //var_dump($final[0]);
      $indented_header   = array(
          0 =&gt; array_keys($final[0])
      );
  
      $re_headered_array = array_merge($indented_header, $final);
  
  
  
      // write array to csv file
      $file2 = fopen($csv_output_file, "w");
  
      foreach ($re_headered_array as $line) {
          fputcsv($file2, $line);
      }
  
      fclose($file2);
  
      }
      }
  </code></pre>
  
  <p>Call to function <code>my_csv_join()</code>:</p>
  
  <pre><code>my_csv_join(array('C:\xampp\htdocs\kalugi\file0.csv','C:\xampp\htdocs\kalugi\file1.csv'), 'C:\xampp\htdocs\kalugi\file2.csv','y','Manufacturer ID','mpn');
  </code></pre>
  
  <p>Actual result shows not all records matching on the <code>file0."Manufacturer ID" = file1.mpn</code> are being matched.  Thus some expected rows that satisfied the join condition were not joined.  We have <code>NULL</code> in place of their values:</p>
  
  <p><code>var_dump</code> of result:</p>
  
  <pre><code>array(3) {
    [0]=&gt;
    array(8) {
      ["Manufacturer ID"]=&gt;
      string(6) "SKU231"
      ["image"]=&gt;
      string(10) "image1.jpg"
      ["description"]=&gt;
      string(6) "A box."
      ["mpn"]=&gt;
      NULL
      ["length"]=&gt;
      NULL
      ["height"]=&gt;
      NULL
      ["width"]=&gt;
      NULL
      ["title"]=&gt;
      NULL
    }
    [1]=&gt;
    array(8) {
      ["Manufacturer ID"]=&gt;
      string(6) "SKUAG1"
      ["image"]=&gt;
      string(11) "image22.jpg"
      ["description"]=&gt;
      string(12) "Another box."
      ["mpn"]=&gt;
      NULL
      ["length"]=&gt;
      NULL
      ["height"]=&gt;
      NULL
      ["width"]=&gt;
      NULL
      ["title"]=&gt;
      NULL
    }
    [2]=&gt;
    array(8) {
      ["Manufacturer ID"]=&gt;
      string(6) "SKU21D"
      ["image"]=&gt;
      string(11) "image7a.png"
      ["description"]=&gt;
      string(12) "A third box."
      ["mpn"]=&gt;
      string(6) "SKU21D"
      ["length"]=&gt;
      string(1) "5"
      ["height"]=&gt;
      string(1) "8"
      ["width"]=&gt;
      string(1) "5"
      ["title"]=&gt;
      string(5) "Box 3"
    }
  }
  </code></pre>
  
  <p>Result as written out to $file2 (file2.csv):</p>
  
  <pre>    +-----------------+-------------+--------------+--------+--------+--------+-------+-------+
      | Manufacturer ID |    image    | description  |  mpn   | length | height | width | title |
      +-----------------+-------------+--------------+--------+--------+--------+-------+-------+
      | SKU231          | image1.jpg  | A box.       |        |        |        |       |       |
      | SKUAG1          | image22.jpg | Another box. |        |        |        |       |       |
      | SKU21D          | image7a.png | A third box. | SKU21D |      5 |      8 |     5 | Box 3 |
      +-----------------+-------------+--------------+--------+--------+--------+-------+-------+
  </pre>
  
  <p>Why are 2 of the rows not joined despite  <code>file0."Manufacturer ID" = file1.mpn</code> being satisfied?</p>