我正在使用一个保存数据的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++;
}