Re: Replacing an element in an array

laredotornado@xxxxxxxxxxx wrote:

I'm using PHP 5. If I have a non-asociative array, that is an array
where the key is the numerical index of the element ...

// For example
$arr = array("Denver", "Chicago", "Seattle", "Austin");

What is the easiest way to replace all instances of an element in the
array with a new element?

Thanks, - Dave

Maybe something like this?


$arr = array("Denver", "Chicago", "Seattle", "Austin");

$keys = array_keys($arr, "Denver");

if ($keys !== false)
foreach($keys as $key)
$arr[$key] = "San Francisco";



Replaces each instance of "Denver" with "San Francisco".

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.