drmy1050
drmy1050
2013-01-07 19:30

PHP如何将我的变量压缩成FOR循环?

已采纳

I have a list of variables which I am using for some PHP functions. I know I can simplify this code but I am not really sure how. I have tried for(){} loops but haven't figured out how to make the result usable.

HERE ARE MY VARIABLES

    $saveData_1  = post_data($url_1);
    $saveData_2  = post_data($url_2);
    $saveData_3  = post_data($url_3);
    $saveData_4  = post_data($url_4);
    $saveData_5  = post_data($url_5);
    $saveData_6  = post_data($url_6);
    $saveData_7  = post_data($url_7);
    $saveData_8  = post_data($url_8);
    $saveData_9  = post_data($url_9);
    $saveData_10 = post_data($url_10);
    $saveData_11 = post_data($url_11);
    $saveData_12 = post_data($url_12);
    $saveData_13 = post_data($url_13);
    $saveData_14 = post_data($url_14);
    $saveData_15 = post_data($url_15);
    $saveData_16 = post_data($url_16);
    $saveData_17 = post_data($url_17);
    $saveData_18 = post_data($url_18);
    $saveData_19 = post_data($url_19);
    $saveData_20 = post_data($url_20);

HERE IS WHAT I HAVE TRIED

for($i = 0; $i<20; $i++) {
        $saveData = '$saveData_'.$i;
        $postData = 'post_data($url_'.$i.')';   
        print($saveData. '=' .$postData. ';');
    }

This works for the most part, it prints out my list of variables. But what I don't know, is how to make this usable so instead of printing them to the page being displayed in the browser they will be instead be "printed" to my php script.

Basically my goal is to write the Variables of above without having to actually write a new variable each time.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • doudichu1358 doudichu1358 8年前

    You need to use arrays. I will type up an example and add it to this comment. But what you want to use is an array variable. One array can hold all this and you can iterate (loop) through it easily.

    Here ya go:

    <?php
    /* what the heck?  You need arrays ;-)
    $saveData_1  = post_data($url_1);
    $saveData_2  = post_data($url_2);
    $saveData_3  = post_data($url_3);
    $saveData_4  = post_data($url_4);
    $saveData_5  = post_data($url_5);
    $saveData_6  = post_data($url_6);
    $saveData_7  = post_data($url_7);
    $saveData_8  = post_data($url_8);
    $saveData_9  = post_data($url_9);
    $saveData_10 = post_data($url_10);
    $saveData_11 = post_data($url_11);
    $saveData_12 = post_data($url_12);
    $saveData_13 = post_data($url_13);
    $saveData_14 = post_data($url_14);
    $saveData_15 = post_data($url_15);
    $saveData_16 = post_data($url_16);
    $saveData_17 = post_data($url_17);
    $saveData_18 = post_data($url_18);
    $saveData_19 = post_data($url_19);
    $saveData_20 = post_data($url_20);
    */
    
    
    
    $saveData = array();  // just a formality. You don't need to delcare variable types in PHP.
    for ($i = 1; $i < 21; $i++) {
        $var = 'url_'.$i;
        $saveData[$i] = post_data($$var);
    }
    
    // now all your stuff is in $saveData
    // what's in savedata 20?  this is:
    echo $saveData[20];
    
    // you could skip all of this and just use the $_POST superglobal
    $_POST[$url_20]
    
    // I'm curious to see what you are doing, because this process should be re-thought.
    
    点赞 评论 复制链接分享
  • duanmaifu3428 duanmaifu3428 8年前

    If I understand what you're asking correctly, you want to have actual "variables" that you can use, such as $saveData_16, but don't want to type each one out?

    If that's the case, you can what's called a variable-variable:

    $varName = 'saveData_16';
    $postName = 'url_16';
    $$varName = post_data($$postName);
    

    Note the double $$ prefixing the variable's name.

    This can be dropped into a loop with:

    for ($i = 0; $i < 20; $i++) {
        $varName = 'saveData_' . $i;
        $postName = 'url_' . $i;
        $$varName = post_data($$postName);
    }
    

    Variable-variables can be tricky to remember and a nuisance to keep up with, especially if they contain similar names/values as existing variables. If they do, they can easily overwrite an important variable in your code. If this is a potential issue, you should probably avoid them.

    An alternative to variable-variables, but on the same topic, if your values really do come from $_POST (based on the function-name post_data()), you can look into PHP's extract() function which performs the same "extraction" for you:

    extract($_POST);
    

    I would recommend against using extract() unless you're very clear on what's needed though. It can easily overwrite other variables that use the same name as a posted field. It does provide an optional second-parameter named $extract_type that is a flag specifying the behavior of the extract and, through this, you can tell it to not overwrite existing variables:

    extract($_POST, EXTR_SKIP);
    

    Again though, this can cause headaches by simply forgetting the optional flag - so I would avoid this function if this is a risk.

    As an additional alternative, you could store the results into an array and use the array as-is. I personally support this method and, if you use strings as your array indexes, it can be just-as-readable as using a variable (and more-readable than a variable-variable).

    If you're currently using $_POST, well, you should be all set - it's already in array form. However, if you're obtaining your data in a different method you can always build your own array with:

    // build your array
    $saveData = array();
    for ($i = 0; $i < 20; $i++) {
        $urlName = 'url_' . $i;
        $saveData['url_' . $i] = post_data($$urlName);
    }
    
    // access a value
    echo $saveData['url_4'];
    

    I'm not 100% sure what your $url_# variables are, so I've kept that as a variable-variable in the array example; if it's just a string, you could replace it with the string instead.

    点赞 评论 复制链接分享

相关推荐