function convert_formula($formula, $formula_array) {
$callback = function($matches) use ($formula_array) {return $formula_array[(int) $matches[0]];};
$converted = preg_replace_callback("/[0-9]+/", $callback, $formula);
return eval("return $converted;");
}
$test_formula = '(3-1) * (3-10)';
$test_formula_array = array(323,67,82,56, 10 => 3);
echo convert_formula($test_formula, $test_formula_array);
Requires PHP 5.3.+ for anon functions. You'll want to first validate the $formula argument to ensure it accepts only numbers and math symbols before you eval it within the function.
Edit: just going by some assumption that this is a tool to instruct students on math and that you already have a pre-built set of formulas to reference... as eval
is one of the most dangerous functions of PHP, perhaps you might consider running this client side using javascript. When you build the page, you simply dynamically create a base reference javascript array containing all possible values to be used in the formula. So this way, using javascript's eval function, the client builds and resolves the formula all on their end. That final evaluation is then passed back to your server which then validates the evaluation and gives a final "correct/wrong" response. Just a thought...