我有100多个if语句。我可以/应该使用foreach或其他工具吗?

时间:2019-08-21 07:02:26

标签: php

我有一个询问用户问题的表格,每个问题都有一个复选框可以选择。

例如

  1. 您想了解苹果吗?”
    • 复选框:“是”
  2. 您想了解紫色吗?
    • 复选框:“是”
  3. 您想了解乔治华盛顿吗?
    • 复选框:“是”

提交表单后,每个复选框都会有一段文本输出。

例如 如果用户检查有关“苹果”的问题和有关“乔治华盛顿”的问题,则在提交表单后,他们将看到:

  • 这是一段有关苹果的文字。
  • 这是关于乔治·华盛顿的一段文字。

如果有代码可以得到我想要的输出,但是涉及到每个问题都有一个if语句-大约有100个问题-所以我想知道是否有更有效/更复杂的方法。

问题字段的元名称和值如下:

  • my_prefix_apples(值:Apples)
  • my_prefix_purple(值:紫色)
  • my_prefix_george_washington(值:乔治华盛顿)

这是我的代码,仅使用上述三个问题作为示例:

// Declare the question metas
$questionMetas = [
    'apples',
    'purple',
    'george_washington',
];

// Save the paragraphs of text that will be output
$applesText = "Here is a paragraph of text about apples.";
$purpleText = "Here is a paragraph of text about purple.";
$georgeText = "Here is a paragraph of text about George Washington.";

// Use table tag to open the table before looping
echo "<table>";

// Loop over all question metas
foreach ($questionMetas as $questionMeta){
    // Save the question values
    $questions = $fields['my_prefix_' . $questionMeta]['value'];

    // If the current field is empty skip to the next field
    if (empty($questions)){
        continue;
    }

    // For the questions that the user selected,
    // output the paragraphs in a table
    if ($questions === Apples){
        echo "<tr>";
            echo "<td>-</td>";
            echo "<td>$applesText</td>";
        echo "</tr>";
    }
    if ($questions === Purple){
        echo "<tr>";
            echo "<td>-</td>";
            echo "<td>$purpleText</td>";
        echo "</tr>";
    }
    if ($questions === 'George Washington'){
        echo "<tr>";
            echo "<td>-</td>";
            echo "<td>$georgeText</td>";
        echo "</tr>";
    }
}

// Close table
echo "</table>";

我一直在尝试用另一个foreach($ questions作为$ question)和一个开关替换100+ if语句,但是我找不到正确的方法。它要么中断,要么不输出任何东西。

2 个答案:

答案 0 :(得分:3)

使用关联数组会更好,所以关键是问题,而值是文本。

$questionMetas = [
    'apples' => "Here is a paragraph of text about apples.",
    'purple' => "Here is a paragraph of text about purple.",
    'george_washington' => "Here is a paragraph of text about George Washington."
];

然后输出仅意味着使用$questions作为键来显示相关文本...

    echo "<tr>";
        echo "<td>-</td>";
        echo "<td>{$questionMetas[$questions]}</td>";
    echo "</tr>";

答案 1 :(得分:1)

无需编写另一个foreach循环,可以在同一循环中处理

 <?php
       // Declare the question metas
    $questionMetas = [
        'apples',
        'purple',
        'george_washington',
    ];

    // Save the paragraphs in the array
    $questionMetasText = [
        'Here is a paragraph of text about apples.',
        'Here is a paragraph of text about purple.',
        'Here is a paragraph of text about George Washington',
    ];

    $counterForTextPosition = 0;
    // Use table tag to open the table before looping
    echo "<table>";

    // Loop over all question metas
    foreach ($questionMetas as $questionMeta){
        // Save the question values
        if(isset($fields['my_prefix_' . $questionMeta]['value'])){
            echo "<tr>";
                echo "<td>-</td>";
                echo "<td>$questionMetasText[$counterForTextPosition] </td>";
            echo "</tr>";
        }

        $counterForTextPosition++;
        // If the current field is empty skip to the next field
        if (empty($questions)){
            continue;
        }
    }

    // Close table
    echo "</table>";
    ?>

进一步,您可以使用关联数组消除2个数组,而只保留一个数组。