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 -
- 1 not "1",
- 53.2 not "53.2",
I created this recursive function that works on mixed type - array/object:
private function cast_number(&$mixed) {
if(is_array($mixed)) {
foreach ($mixed as $key => $val)
if (is_numeric($val))
$mixed[$key] = (double)$val;
else if (is_array($val) || is_object($val))
$mixed[$key] = $this->cast_number($val);
} else if(is_object($mixed)) {
foreach ($mixed as $key => $val)
if (is_numeric($val))
$mixed->$key = (double)$val;
else if (is_array($val) || is_object($val))
$mixed->$key = $this->cast_number($val);
}
return $mixed;
}
Pretty simple function - if is number, cast double, if is array or object, go over recursivly.
Everything here is in place.
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..
- How can I improve this function? (Speed, Memory)
- Why is it taking so long? even json_encode, that I would think is a bigger function takes much less time..