string_replace在循环中替换第一个段

This question is based on my previous question. This is my code.

$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)
    }
`

So my problem is as I mentioned in the code I am always getting (12 > 4) OR (12 > 2).
So the output is,

@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)`

Even I am getting $cond correctly while debugging couldn't replace the relevant $cond in iteration. I couldn't understand why ? please help me.

You have to define a new variable $newlogicexpression inside the iteration, otherwise after the first iteration you don't have (#A) OR (#B) in $logicexpression anymore and the replace won't work...

$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);
}