Forum Moderators: coopster

Message Too Old, No Replies

replace array items with other items

hopefully simple question!

         

HelenDev

11:03 am on Jul 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have an array e.g.

$fruit= array("apple", "orange", "pear");

and I want to replace one value with another, say orange with banana, and keep the order (keys) of the array the same.

I'm sure this should be simple but I just can't fathom it!

Habtom

12:10 pm on Jul 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>$fruit= array("apple", "orange", "pear");

$fruit['1'] = "Banana";

Hab

HelenDev

1:04 pm on Jul 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Hab, but what if I don't know which number orange will be? It could appear anywhere in the array.

jatar_k

1:06 pm on Jul 10, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what about using
[php.net...]

then use the returned key to swap the value

HelenDev

1:59 pm on Jul 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Aha, perfect, thanks :)