Page 1 of 1

how to pop an element off ANYWHERE in an array?

Posted: Fri Feb 17, 2006 4:07 pm
by Deseree
http://us3.php.net/manual/en/ref.array.php

I see array_pop() but that only does it for the last element of an array.

I am going through an array like this:

Code: Select all

for($kount=0;$kount<sizeof($ps_list);$kount++)
						{
							$pxy = explode("|",$ps_list[$kount]);
							$pxy = $pxy[0];
								
							if($pxy[$kount] == $this->C_PS)
							{
								$ps_list[$kount] = "";
						

							}
						}
And $this->C_PS is the element of an array I need to pop. This so far removes it from the .txt file but not from the array..... Any ideas how I would remove this element from the array? unset() I'm guessing that wont' work?

Posted: Fri Feb 17, 2006 4:12 pm
by feyd
array_splice() or array_slice()
unset() works too if you don't want what was in the element(s) you remove.

Posted: Fri Feb 17, 2006 4:16 pm
by Deseree
thanks/sorry bout double post accident.

Posted: Fri Feb 17, 2006 4:20 pm
by Deseree
mmk, I definately don't want what was in the element of the array, but another part of my script/function is transfeversing the array by using current() and next(), and also reset() , will it be a problem if I use unset on this SINGLE element, when it resets and comes back to this element, will it show up as blank, or does unset remove the KEY and the VALUE from the array, so when it comes back around with current() and next(), it will just SKIP to the next position automatically, or will i need to resort somehow ( maintaining current order of course)?

Posted: Fri Feb 17, 2006 4:24 pm
by feyd
current(), next(), reset(), and rewind() are not affected by unset() when it is run prior. I haven't tested unset() during their usage.

Posted: Fri Feb 17, 2006 4:42 pm
by Deseree
bummer, didnt work.

I can't use array splice or array slice since I don't have the position, unless array_keys() returns that... let me try that...