Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder zro@rtv's Avatar
    Join Date
    Feb 2005
    Location
    on the network
    Posts
    433
    Thanks
    0
    Thanked 1 Time in 1 Post

    Switch List vars in Foreach

    I just know I'm being stupid here, but I can;t seem to kick my brain into thinking of wtf is wrong with my logic.

    functionality im trying to achieve:
    PHP Code:
    <?php

    function do_something($opt$file){

        
    $lines gzfile($file);
        foreach (
    $lines as $line){
            switch (
    $opt){
                case 
    1:
                    list(
    $foo$bar) = explode(','$line);
                    break;
                case 
    2:
                    list(
    $foo$bar$baz) = explode(','$line);
                    break;
                case 
    3:
                    list(
    $foo$bar$baz$boom) = explode(','$line);
                    break;
            }
        }
    }

    ?>
    This seems ridiculous to sets the case on every line.
    rather it would make sense to test case before the foreach and then pass the vars to list? But I can't think of how to do this for some reason?

    I can't wait to see the obvious answer to this and kick myself hard.
    ._-zro
    zro@redtv
    zro.redtv.org

    "If HTML and the Web made all the online documents look like one huge book, RDF, schema, and inference languages will make all the data in the world look like one huge database"
    -Tim Berners-Lee, Weaving the Web, 1999

  • #2
    Regular Coder zro@rtv's Avatar
    Join Date
    Feb 2005
    Location
    on the network
    Posts
    433
    Thanks
    0
    Thanked 1 Time in 1 Post
    ok, so I think I worded that poorly.
    obviously I believe this is better than what I had posted:

    PHP Code:
    <?php

    function do_something($opt$file){

        
    $lines gzfile($file);

            switch (
    $type){
                case 
    1:
                    foreach (
    $lines as $line){
                        list(
    $foo$bar) = explode(','$line);
                    }
                    break;
                case 
    2:
                    foreach (
    $lines as $line){
                        list(
    $foo$bar$baz) = explode(','$line);
                    }
                    break;
                case 
    3:
                    foreach (
    $lines as $line){
                        list(
    $foo$bar$baz$boom) = explode(','$line);
                    }
                    break;
            }
        }
    }
    ?>
    But that seems redundant.
    Wha tI'm looking for is a more elegant construct something that perhaps lokos more like this.

    PHP Code:

    <?php

    function do_something($opt$file){

            switch (
    $opt){
                case 
    1:
                   
    AUTO-MAGICAL
                    
    break;
                case 
    2:
                   
    AUTO-MAGICAL
                    
    break;
                case 
    3:
                   
    AUTOMAGICAL
                    
    break;
            }

        
    $lines gzfile($file);
        foreach (
    $lines as $line){
             list(
    AUTO-MAGICAL) = explode(','$line);
        }
    }

    ?>
    ._-zro
    zro@redtv
    zro.redtv.org

    "If HTML and the Web made all the online documents look like one huge book, RDF, schema, and inference languages will make all the data in the world look like one huge database"
    -Tim Berners-Lee, Weaving the Web, 1999


  •  

    Posting Permissions

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