这个问题是基于我之前的问题。这是我的代码。
$this->logger->debug($datauser); // [ZN1961] => Array([#A] => 12 > 4 [#B] => 12 > 2 ), [ZN1962] => Array ([#A] => 20 > 4 [#B] => 20 > 2 )
logicexpression = ((#A) OR (#B) );
$newArr = array();
foreach ($datauser as $key => $value) {
foreach ($value as $logname => $cond) {
$this->logger->debug($cond); /// WHEN DEBEG HERE it gives correctly the $cond in each iteration
$logicexpression =str_replace($logname, $cond, $logicexpression);
}
$this->logger->debug($logicexpression ); // here I am always getting (12 > 4) OR (12 > 2)
}
`
所以我的问题就像我在代码中提到的那样(12> 4)或(12> 2)。
所以输出是,
@547,12 > 4
@547, 12 > 2
@557, (12 > 4) OR (12 > 2)
@547, 20 > 4
@547, 20 > 2
@557, (12 > 4) OR (12 > 2) // here it should has to come as (20 > 4) OR (20 > 2)`
即使我正在调试$cond
,调试也无法在迭代中替换相关的$cond
。我无法理解为什么?请帮我。
答案 0 :(得分:1)
你必须在迭代中定义一个新变量($ newlogicexpression),否则在第一次迭代之后你不再在$ logicexpression中有((#A)OR(#B))并且替换将不起作用...
$logicexpression = ((#A) OR (#B) );
foreach ($datauser as $key => $value) {
$newlogicexpression = $logicexpression;
foreach ($value as $logname => $cond) {
$newlogicexpression = str_replace($logname, $cond, $newlogicexpression);
}
$this->logger->debug($newlogicexpression);
}