Switch two items in associative array PHP

Posted by user280381 on Stack Overflow See other posts from Stack Overflow or by user280381
Published on 2010-03-15T15:37:07Z Indexed on 2010/03/15 15:39 UTC
Read the original article Hit count: 176

Filed under:
|

Example:

$arr = array('apple'=>'sweet','grapefruit'=>'bitter','pear'=>'tasty','banana'=>'yellow');

I want to switch the positions of grapefruit and pear, so the array will become

'apple'=>'sweet','pear'=>'tasty','grapefruit'=>'bitter','banana'=>'yellow')

I know the keys and values of the elements I want to switch, is there an easy way to do this? Or will it require a loop + creating a new array?

Thanks

© Stack Overflow or respective owner

Related posts about php

Related posts about arrays