Jump to content

Archived

This topic is now archived and is closed to further replies.

bcamp1973

removing selected value from array

Recommended Posts

So, i have an array...

[code]$arr => ('a','b','c')[/code]

...and i want to remove one value from that array before submitting it to a function so it will look like...

[code]Array => ('a','c')[/code]

Is this possible?  I can only find functions to strip the beginning/end off an array, but nothing in between :(

Share this post


Link to post
Share on other sites
I've been wanting to know how to do that too. The only thing I could think of is using serialize and then trying to take it out. But i'm stumped there, too.

You can use in_array() to detect it, but not remove.

Share this post


Link to post
Share on other sites
if you have an array

$blah = array('a','b','c');

you would access for instance 'b' with $blah[1] since array positions start at zero.  so if you wanted to delete 'b' from that array you would do this:

unset ($blah[1]);

Share this post


Link to post
Share on other sites
Perfect!  Thanks Caryon Violent...you've saved me more than once on here :)  I was imploding the array, stripping the value using str_replace, then exploding it again...exceptionally sloppy I know!

Share this post


Link to post
Share on other sites
but just so you know, if you do that, even though you will end up with an array of array('a','b') your keys will be (0,2) not (0,1) if you wish for there not to be a gap, then after unsetting 'b', you will want to do this:

$blah = array_values($blah);

this will re-index your array.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.