title: How to find out why json_encode is failing (PHP)?
body: <p>I have huge csv with a list of names and dates. I need to put a subset of these into a PHP struct for a given month. So I run through the CSV and filter those rows where the months is what I desired submit the names to a list and then save that list to a file using json_encode.</p>
  <p>I do this once for every month for a total of 12 times as I just need one year.</p>
  <p>I did months 1 through 7 with NO issue. Now month 8 fails. The list is generated but nothing is stored because the json_encode fails.</p>
  <p>Calling on json_last_error_msg() provides the message Malformed UTF-8 characters.</p>
  <p>I read the CSV by doing</p>
  <pre><code>  if (($handle = fopen($in_csv, &quot;r&quot;)) !== FALSE) {
         while (($data = fgetcsv($handle, 0, &quot;,&quot;)) !== FALSE) {           
             $line++;
             
             $data = array_map(&quot;utf8_encode&quot;, $data);  // &lt;- This was added after the first failure. But I have tried without it
  
             // ... do stuff with data and save the name and last name to a struct. 
             
         }       
         fclose($handle);
     }
  
     // At the end I do
     $fid = fopen(&quot;filename&quot;,&quot;w&quot;);
     fwrite($fid,json_encode($output));
     echo &quot;JSON ENCODE RESULT: &quot; . json_last_error_msg() . &quot;\n&quot;;
     fclose($fid);
  </code></pre>
  <p>I really need this to work. Is thre ANY way that JSON can tell me what it doesn't like so I can delete it or correct it?</p>