Jump to content


Photo

removing selected value from array


  • Please log in to reply
4 replies to this topic

#1 bcamp1973

bcamp1973
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 14 July 2006 - 03:16 AM

So, i have an array...

$arr => ('a','b','c')

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

Array => ('a','c')

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

#2 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 14 July 2006 - 03:27 AM

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.

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#3 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 14 July 2006 - 03:35 AM

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]);


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#4 bcamp1973

bcamp1973
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 14 July 2006 - 03:38 AM

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!

#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 14 July 2006 - 03:41 AM

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users