0

我正在使用一个保存数据的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缩短它
这个例子显然不起作用......需要让它工作
我尝试使用“variables varible”来做这个并且失败了......

        $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++;
}   
4

1 回答 1

2

您可以使用变量 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];
    }

}

我上面所说的例外情况是,如果您将大量相似的数据插入到同一个表中,您可能希望通过使用一个插入查询来加速您的脚本,在这种情况下使用上面的数组选项,然后$str_of_values = implode("','", $array);调用获取一个可以插入 MySQL 查询的字符串...VALUES('$str_of_values'),如下所示:

取决于你所追求的,但变量变量会分配你在顶部的长列表并节省你的文件大小......

于 2013-10-23T02:11:00.350 回答