Setting New values on foreach loop

I am trying to loop thru an array containing several objects and changing the value of the misspelled objects to to correct spelling to work with later on, but I haven’t been able to set the new value for each object the loop is on… I figure the code below is the start of it, if anyone could help me out with setting each new value and saving it back in the array that would be much appreciated. Thanks


foreach($cities as $city) {
	if(($city->loc_city) == "misspelling") {
		// insert new value for loc_city;
	}
		
}

$city is a temporary variable.

To modify the actual array, you must use

foreach ($cities as $key=>$city)
{
$city->loc_city = ‘new value’;
$cities[$key] = $city;
}

If $city is an object then it is always passed by reference. Thus you can modify it to your hearts content and it will update all references.