title: Changing character encoding of multidimensional array
body: <p>I have a multidimensional array that looks something like this:</p>
  
  <pre><code>ourThing = array(
      'id' =&gt; 1,
      'title' =&gt; 'foo',
      'data' =&gt; array(
          'name' =&gt; 'bar',
          'metadata' =&gt; array(
              'time' =&gt; '2011-02-01 12:00:00'
          )
      )
  );
  </code></pre>
  
  <p>Now, because I have to use <code>json_encode</code> and <code>json_decode</code> on them, I need to store at least the stuff in <code>data</code> as UTF-8. Unfortunately, the website uses windows-1252, and that's something I can't change. Because I might want to add even more levels to the array (within data) in the future, I figured I'd change the encoding recursively, like so:</p>
  
  <pre><code>function encode_items($arr) {
      foreach ($arr as $n =&gt; $v) {
          if (is_array($v))
              encode_items($arr[$n]);
          else
              $arr[$n] = mb_convert_encoding($v, 'Windows-1252', 'UTF-8');
      }
  }
  </code></pre>
  
  <p>However, this is not working. If I print $arr[$n] right after encoding it, it comes out right, but the original array doesn't seem to change, because when I later try to print out the values from the array, I get character encoding issues.</p>
  
  <p>tl;dr: I need to change the encoding of the information in <code>ourThing['data']</code> from utf-8 to windows-1252.</p>
  
  <p>How can I make it so that the original array is changed?</p>
  
  <p>EDIT: Thanks to a helpful commenter, I now know what I was doing wrong. I forgot to actually return the array after doing the encoding. Here's a working example:</p>
  
  <pre><code>ourArray = array(
      'id' =&gt; 1,
      'title' =&gt; 'foo',
      'data' =&gt; array(
          'name' =&gt; 'bar',
          'metadata' =&gt; array(
              'time' =&gt; '2011-02-01 12:00:00'
          )
      )
  );
  
  function encode_items($arr) {
      foreach ($arr as $n =&gt; $v) {
          if (is_array($v)) {
              $arr[$n] = encode_items($v);
          } else {
              $arr[$n] = mb_convert_encoding($v, 'Windows-1252', 'UTF-8');
          }
      }
      return $arr;
  }
  
  $ourArray = encode_items($ourArray);
  </code></pre>