简体   繁体   English

PHP循环...需要一些建议

[英]PHP Looping…need some advice

I have the following code: 我有以下代码:

$q1 = $_POST["q1"];
$q2 = $_POST["q2"];
$q3 = $_POST["q3"];
$q4 = $_POST["q4"];
$q5 = $_POST["q5"];
$q6 = $_POST["q6"];
$q7 = $_POST["q7"];
$q8 = $_POST["q8"];

At the moment, this is hard coded and I need to manually change it each time, I'd like to use variables instead so that it's not a manual process. 目前,这是硬编码的,我需要每次手动更改它,我想改用变量,这样它就不是手动过程。

Is it a case of using a loop, while or foreach? 是在while或foreach中使用循环吗?

If I had the the information $q and q in an array would that help? 如果我将信息$ q和q放在一个数组中会有所帮助吗?

Thanks, 谢谢,

Homer. 荷马

Consider adjusting your forms to use Array notation, eg 考虑调整表单以使用数组符号,例如

<ul>
    <li><input name="q[]" /></li>
    <li><input name="q[]" /></li>
    <li><input name="q[]" /></li>
    <li><input name="q[]" /></li>
    ...
</ul>

This would make $_POST['q'] contain an array with all input values given for 'q', which you can then easily iterate over with foreach like this: 这将使$_POST['q']包含一个数组,其中包含所有为'q'给出的输入值,然后您可以轻松地使用foreach进行迭代,如下所示:

foreach($_POST['q'] as $q) {
    // do something with $q
}

See http://www.johnrockefeller.net/html-input-forms-sending-in-an-array-in-php/ 参见http://www.johnrockefeller.net/html-input-forms-sending-in-an-array-in-php/

Yes this is the time for a loop. 是的,这是循环的时候了。 You can use foreach or while, it does not really matter. 您可以使用foreach或一会儿,这并不重要。

$i = 1; 
$q = array(); 
while($i < 9) {
    $q[$i] = $_POST["q" . $i];
    $i += 1;
}

1. 1。

$keys = array('q1', 'q2', 'q3', 'q5', 'q9');
$q = array();
foreach ( $keys as $key ) {

  $q[$key] = isset($_POST[$key]) ? $_POST[$key] : null;
}

2. 2。

$keys = array('q1', 'q2', 'q3', 'q5', 'q9');
foreach ( $keys as $key ) {
  $$key = isset($_POST[$key]) ? $_POST[$key] : null;
}
// in output you will have variables called $q1, $q2, $q3, ...

3. 3。

$amount = 8;
$q = array();
for ( $i = 1; $i <= $amount; ++$i ) {
  $q[$i] = isset($_POST['q' . $i]) ? $_POST['q' . $i] : null;
}

Untested, and it's been a while since I last used PHP: 未经测试,自从我上次使用PHP已经有一段时间了:

$q = array();
for ($i = 1; $i <= 8; ++$i)
    $q[$i] = $_POST["q" . $i];

Try following code, in case if it's ok with you to save POST data to another array: 如果可以将POST数据保存到另一个数组,请尝试以下代码:

// Random POST array
$_POST["q1"] = 1;
$_POST["q2"] = 2;
$_POST["q3"] = 3;
$_POST["q4"] = 4;
$_POST["q5"] = 5;
$_POST["q6"] = 6;
$_POST["q7"] = 7;
$_POST["q8"] = 8;

$array = Array( );

foreach ( $_POST as $value ) {
    $array[ ] = $value;
}

In case you want to save POST data into more specific variables, you'll have to use bit more complex piece of code. 如果要将POST数据保存到更特定的变量中,则必须使用一些更复杂的代码。 Can edit my post if you want to see more options. 如果您想查看更多选项,可以编辑我的帖子。

Edit: 编辑:

If you wanted to work just with keys that start with q and end with number, you could use following code: 如果您只想使用以q开头和以数字结尾的键,则可以使用以下代码:

$array = Array( );

foreach ( $_POST as $key => $value ) {
    if ( preg_match( "/^[q]{1}\d$/", $key ) ) {
        $array[ ] = $value;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM