Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stupid Array Question

Status
Not open for further replies.

GiffordS

Programmer
Joined
May 31, 2001
Messages
194
Location
CA
Ok, so I haven't been working in PHP for awhile and I'm now a bit rusty. Here's the deal:

I have a variable that is also part of an array. I need to remove the variable from the array without unsetting the variable.

Anyone?
 
Ok, let's say I have a an array, and that array contains three elements, "bob", "joe", and "ed". Now, let's say I want to remove the element "bob" so that my array only contains "joe" and "ed". I know this is a really simple question, but as I said I haven't worked in PHP in quite awhile and I'm just a bit rusty. I've looked through the manual and found all kinds of functions to sort etc, but I cannot remember how to just drop an element from an array.
 
How 'bout:

If the array looks something like:

$foo = array (0 => 'bob', 1 => 'joe', 2 => 'ed');

Then to remove the element containing 'joe', perform: unset ($foo[1]);

(
If you need to find the element that contains "joe", use array_search() ( but read the page on array_search() and pay careful attention to the warning on that page.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Ok, this is the other half of the question. I also have a variable, independent of the array, $g="bob". I was just afraid that by unsetting the "bob" element from the array that I would also unset the $g variable. As I write this I realize that to unset the $g variable I would actually need to unset($g). Stupid question. I hate not remembering stuff like that. Thanks for the response.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top