title: PHP - Recursive in place replacing of strings to numbers takes too long
body: <p>Because MYSQL's "SELECT" selects integers and floats as strings, and I need every response I get (from JS) to be in a correct data model -</p>
  
  <ul>
  <li>1 not "1",</li>
  <li>53.2 not "53.2", </li>
  </ul>
  
  <p>I created this recursive function that works on mixed type - array/object:</p>
  
  <pre><code>private function cast_number(&amp;$mixed) {
      if(is_array($mixed)) {
          foreach ($mixed as $key =&gt; $val)
              if (is_numeric($val))
                  $mixed[$key] = (double)$val;
              else if (is_array($val) || is_object($val))
                  $mixed[$key] = $this-&gt;cast_number($val);
      } else if(is_object($mixed)) {
          foreach ($mixed as $key =&gt; $val)
              if (is_numeric($val))
                  $mixed-&gt;$key = (double)$val;
              else if (is_array($val) || is_object($val))
                  $mixed-&gt;$key = $this-&gt;cast_number($val);
      }
      return $mixed;
  }
  </code></pre>
  
  <p>Pretty simple function - if is number, cast double, if is array or object, go over recursivly.</p>
  
  <p>Everything here is in place.</p>
  
  <p>I have two problems with this:
   - On 6MB of data, of mostly numbers that are represented as strings it took 0.5 seconds
   - On 200MB of data (yes, I need it, please don't focus on it), it failed after a couple of minutes (usually seconds), saying it needs more than 4GB of memory..</p>
  
  <ol>
  <li>How can I improve this function? (Speed, Memory)</li>
  <li>Why is it taking so long? even json_encode, that I would think is a bigger function takes much less time..</li>
  </ol>