SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)

    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?

  2. #2
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Brute force....
    PHP Code:
    <?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
    Denny Schlesinger
    web services

  3. #3
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)
    I suppose I could write up a class for this. I was hoping there might have been something cooler someone stumbled upon.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •