PHP if语句为变量

时间:2015-10-13 06:31:59

标签: php

我有一个变量($ statement),它将存储动态内容,如下所示:

($row[Gender] == "Female") && ($row[Grade] == "A" || $row[Grade] == "B" || $row[Grade] == "C1") && ($row[Race] == "African" || $row[Race] == "Coloured" || $row[Race] == "Indian" || $row[Race] == "White")

我想在if条件中使用此$statementif ($statment) {do logic here...}。这用于匹配$ row中保存的数组中的值(例如$row[Gender],其值为" Male")。

这是可以实现的吗?最好的方法是什么?

整个方法()是:

public function ApplyRuleset ($activeOpportunity, $step, $companyId, $projectId, $phaseId) {
    // Get global vars
    global $conn;

    // Get non declined employees for this opportunity only
    $getAllCandidates = $this->GetTalentPool ("normal", $step, $activeOpportunity[0]['oppID'], $companyId, $projectId, $phaseId);

    // Get column slices
    $columnSlices = GetSlices ($conn, $companyId, $projectId, $phaseId, 0);

    // Split ruleset into manageable parts
    $columnSets = explode (" AND ", $activeOpportunity[0]['rule']);

    echo "<pre>";

    // Check that we have all that we need
    if (is_array ($getAllCandidates) && is_array ($columnSlices) && is_array ($columnSets)) {

        // Get selected talent pool
        foreach ($getAllCandidates as $row) {
            // Format business rules
            foreach ($columnSlices as $sliceValue) {
                // Get the slices to match which facets were selected
                $qualifiedName = preg_replace ('/\s+/', "", $sliceValue['facet']);

                // Loop through column match
                foreach ($columnSets as $set) {
                    // Get olumn match
                    $ruleMatch = explode (" -> ", $set);
                    $columnName = $ruleMatch[0];
                    $options = explode (",", $ruleMatch[1]);

                    // Match rule
                    for ($i = 0; $i < count ($options); $i++) {
                        // Write conditional statement
                        $statement .= '("$row[' . $columnName . ']" == "' . $options[$i] . '"';
                        $statement .= (count ($options) > 1 && $options[$i] !== end ($options)) ? " || " : ") && (";
                    }
                }
                break;
            }

            // Finalise statement
            $editStatement = preg_replace ("/ && \($/", "", $statement);
            $editStatement = preg_replace ("/ \(/", " ", $editStatement);
            $editStatement = preg_replace ('/"\$/', "$", $editStatement);
            $finaliseStatement = preg_replace ('/\]"/', "]", $editStatement);

            // Do record match
            if ($finaliseStatement) {

                // Record matches, load into match array
                $this->rulesetMatch[] = $row;

                // Set required vars
                $_REQUEST['selected'] = (array) $row[candidateId];
                $_REQUEST['opportunityID'] = $activeOpportunity[0]['oppID'];
                $_REQUEST['step'] = $step;

                // Insert these records for round 1
                //$this->AddCandidatesToOpportunity ();
            }
        }
    }

    // Return all the rows that matched and dedupe
    return array_map ("unserialize", array_unique (array_map ("serialize", $this->rulesetMatch)));
}

1 个答案:

答案 0 :(得分:1)

您可以在名为$ statement的变量中使用if的结果,如

$statement = (($row[Gender] == "Female") && ($row[Grade] == "A" || $row[Grade] == "B" || $row[Grade] == "C1") && ($row[Race] == "African" || $row[Race] == "Coloured" || $row[Race] == "Indian" || $row[Race] == "White"));

然后你可以在if语句中使用这个结果

if ($statement === true) {
  // ...
}