Automatic type casting

So I’m wondering if anyone has a method or stumbled across something that will read a variable and recast it on the fly? I’m starting to get into more and more data thats being spooled from a text file and the type is always cast as a string coming from there. Most of these operations are being pointed to MongoDB, and as some of you may know, the type that is cast in PHP is the type that is created in Mongo.

So essentially I want to do a foreach() on an array and dynamically recast to integers / nulls when possible. Ideas?

Brute force…

<?php

foreach($array AS $key => $value) {

    if (is_null($value)) {
        settype($array($key), 'null');
    } elseif (is_integer($value)) {
        settype($array($key), 'integer');
    } etc.....
}

?>

http://www.php.net/manual/en/function.settype.php

I suppose I could write up a class for this. I was hoping there might have been something cooler someone stumbled upon.