drmy1050 2013-01-07 19:30
浏览 13
已采纳

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 2013-01-07 19:33
    关注

    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.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?