将foreach中的数组值与另一个数组中的键/值相关联

Say you have the array .

$arr = array('foo' => 'bar, 'wang' => 'chung', 'ying' => 'yang');

Now I want to loop through another array (var = $terms) to get values using foreach. If the value is any of the keys listed in $arr, I want to replace it with the value listed in $arr.

I've tried this ...

foreach($terms as $term => $arr) {
echo $term[$arr];
}

This doesn't work ... and I'm pretty stumped beyond that point. Reading through the manual on foreach ... I felt like this was on the right path - but think I'm needing a nudge in another direction.

Thoughts?

You can use array_key_exists() function for verifying whether key exists in another array or not If found then replace that with existing once. You can refer below answer,

$arr = array('foo' => 'bar', 'wang' => 'chung', 'ying' => 'yang');

$res = [];
foreach($terms as $term => $arr1) {
   if( array_key_exists( $term, $arr ) ) {
      $res[$term] = $arr[$term];
    } else {
      $res[$term] = $arr1;
    }
 }
echo '<pre>'; print_r($res);

I hope this will resolve your problem.