循环中的代码更短

时间:2013-10-23 02:03:02

标签: php variables

我正在使用一个保存数据的while循环...我想缩短冗长的代码
我需要为每个项目编写并使用$ counter值作为变量的一部分。

我会自己搜索和测试,但我不知道如何定义搜索 所以我试着解释....

说我需要做这样的事情:

    $xvalue_1 = $_POST['VALUE 1'];
    $xvalue_2 = $_POST['VALUE 2'];
    $xvalue_3 = $_POST['VALUE 3'];
    $xvalue_4 = $_POST['VALUE 4'];
    $xvalue_5 = $_POST['VALUE 5'];

    $yvalue_1 = $_POST['VALUE 1'];
    $yvalue_2 = $_POST['VALUE 2'];
    $yvalue_3 = $_POST['VALUE 3'];
    $yvalue_4 = $_POST['VALUE 4'];
    $yvalue_5 = $_POST['VALUE 5'];

    $zvalue_1 = $_POST['VALUE 1'];
    $zvalue_2 = $_POST['VALUE 2'];
    $zvalue_3 = $_POST['VALUE 3'];
    $zvalue_4 = $_POST['VALUE 4'];
    $zvalue_5 = $_POST['VALUE 5'];

    // SAVING HERE
    some_save_todb_func($xvalue_1);
    some_save_todb_func($xvalue_2);
    some_save_todb_func($xvalue_3);
    some_save_todb_func($xvalue_4);
    some_save_todb_func($xvalue_5);

    some_save_todb_func($yvalue_1);
    some_save_todb_func($yvalue_1);
    some_save_todb_func($yvalue_1);
    some_save_todb_func($yvalue_1);
    some_save_todb_func($yvalue_1);

    some_save_todb_func($zvalue_1);
    some_save_todb_func($zvalue_1);
    some_save_todb_func($zvalue_1);
    some_save_todb_func($zvalue_1);
    some_save_todb_func($zvalue_1);


如何使用foreach缩短它
这个例子显然不起作用......需要使它工作 我尝试使用“变量变量”并且失败了......

        $xvalue_1 = $_POST['VALUE 1'];
    $xvalue_2 = $_POST['VALUE 2'];
    $xvalue_3 = $_POST['VALUE 3'];
    $xvalue_4 = $_POST['VALUE 4'];
    $xvalue_5 = $_POST['VALUE 5'];

    $yvalue_1 = $_POST['VALUE 1'];
    $yvalue_2 = $_POST['VALUE 2'];
    $yvalue_3 = $_POST['VALUE 3'];
    $yvalue_4 = $_POST['VALUE 4'];
    $yvalue_5 = $_POST['VALUE 5'];

    $zvalue_1 = $_POST['VALUE 1'];
    $zvalue_2 = $_POST['VALUE 2'];
    $zvalue_3 = $_POST['VALUE 3'];
    $zvalue_4 = $_POST['VALUE 4'];
    $zvalue_5 = $_POST['VALUE 5'];

    $counter = 1;
    while($counter < 5) {

        // TRYING TO USE THE COUNTER AS VARIABLE 
        // WHICH IS PART OF THE VARIABLE...
        some_save_todb_func($xvalue_{$counter});
        some_save_todb_func($yvalue_{$counter});
        some_save_todb_func($zvalue_{$counter});

        $counter++;
    }   

我没有正式的PHP培训,所以如果我的问题看起来很愚蠢,我会道歉。 会在这里帮助你...

编辑1(希望最后编辑)

$xvalue_1 = 'xvalue 1 var';
$xvalue_2 = 'xvalue 2 var';
$xvalue_3 = 'xvalue 3 var';
$xvalue_4 = 'xvalue 4 var';
$xvalue_5 = 'xvalue 5 var';

$yvalue_1 = 'yvalue 1 var';
$yvalue_2 = 'yvalue 2 var';
$yvalue_3 = 'yvalue 3 var';
$yvalue_4 = 'yvalue 4 var';
$yvalue_5 = 'yvalue 5 var';

$zvalue_1 = 'zvalue 1 var';
$zvalue_2 = 'zvalue 2 var';
$zvalue_3 = 'zvalue 3 var';
$zvalue_4 = 'zvalue 4 var';
$zvalue_5 = 'zvalue 5 var';

$counter = 1;
while($counter < 6) {

    $xvalue =   'xvalue_'.$counter;
    $yvalue =   'yvalue_'.$counter;
    $zvalue =   'zvalue_'.$counter;

    echo $$xvalue."<br />";
    echo $$yvalue."<br />";
    echo $$zvalue."<br />";

    $counter++;
}   

1 个答案:

答案 0 :(得分:2)

您可以使用variable variables,或者创建一个数组(然后循环遍历该数组)来执行此操作。看起来如果你的循环控制结构正确,你也可以在循环中调用数据库保存函数,而不是分配变量。

$letters = array('x', 'y', 'z');
$max_number 5;

$array = array();

foreach($letters as $letter) {

    for($i = i; $i <= 5; $i++) {
        // set variable variable
        $var_name = $letter . 'value_' . $i;
        $$var_name = $_POST['VALUE ' . $i];
        // you probably won't need to do that using these loops though, just call the function:
        some_save_todb_func( $_POST['VALUE ' . $i] );
        // ... or set array value
        $array[$varname] = $_POST['VALUE ' . $i];
    }

}

我所说的顶部的例外是如果你在同一个表中插入大量类似的数据,你可能想要通过使用一个插入查询来加速你的脚本,其中case使用上面的数组选项,然后调用$str_of_values = implode("','", $array);得到一个字符串,你可以在这样的MySQL查询中插入:...VALUES('$str_of_values')

取决于您所追求的内容,但变量变量将指定您已经占据最上面的长列表并保存文件大小......