PHP adding or subtracting elements from array -


i have query string. example:

?filters=1,2,3,4 

it gets turned array:

$filters = explode(',', $_get['filters']); 

you push new value on

$filters = array_push($filters, $new->filter); 

then turn query string

http_build_query($filters); 

or, remove value

$filters = array_diff($filters, [$new->filter]); 

then turn query string

http_build_query($filters); 

i'm looking elegant solution remove item if exists or add item if not exist. alternative solutions welcome.

thank you.

hopefully i'm understanding correctly "i'm looking elegant solution remove item if exists or add item if not exist.". also, not sure if elegant may spark other ideas:

$filters = in_array($new->filter, $filters) ?     array_diff($filters, [$new->filter]) :     array_merge($filters, [$new->filter]); 

Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -