mathString=′goodsprice−costprice′;array = ['goods_price' => 1000, 'cost_price' => 500];
// 解析数学运算字符串
pattern=′/([a−zA−Z]+)/′;//匹配字母和下划线pregmatchall(pattern, mathString,matches);
variables=matches[0];
// 获取对应的值
values=[];foreach(variables as variable) {
if (isset(array[variable])) {
values[] = array[variable];
}
}
// 执行数学运算
result=eval("return".strreplace(variables, values,mathString) . ";");
dd($result)