Connecting Tech Pros Worldwide Help | Site Map

Array Remove

  #1  
Old September 30th, 2005, 12:35 PM
thehuby
Guest
 
Posts: n/a
Is there an acutal function in PHP for removing an element of an array?

eg: array1 = ("zero", "one", "two", "three");
array1 = remove_element( array1, 2 );
// array1 now contains ("zero", "one", "three");

I have been trying the unset method but this seems to be corrupting my
array (which is holding a set of objects);

I can knock something together with aray_merge, array_splice and
similar but just didn't want to reinvent the wheel.

Thanks,

Rick
www.e-connected.com

  #2  
Old September 30th, 2005, 01:35 PM
Oli Filth
Guest
 
Posts: n/a

re: Array Remove


thehuby said the following on 30/09/2005 12:29:[color=blue]
> Is there an acutal function in PHP for removing an element of an array?
>
> eg: array1 = ("zero", "one", "two", "three");
> array1 = remove_element( array1, 2 );
> // array1 now contains ("zero", "one", "three");
>
> I have been trying the unset method but this seems to be corrupting my
> array (which is holding a set of objects);
>[/color]

Unset() is the method given in the manual.

In what way is it corrupting your array?


--
Oli
  #3  
Old September 30th, 2005, 02:45 PM
Chung Leong
Guest
 
Posts: n/a

re: Array Remove


What's wrong with using array_splice($array, $index, 1)?

  #4  
Old September 30th, 2005, 02:55 PM
thehuby
Guest
 
Posts: n/a

re: Array Remove


I was getting a message basically telling me that PHP could not use my
objects corectly. I got it working by using array_merge after unset.

No idea why it suddenly works after array_merge though but it will do.

My question was really just if anyone knew of a method/funciton to
remove them without having to mess around with splitting and merging
arrays.

  #5  
Old September 30th, 2005, 04:25 PM
comp.lang.php
Guest
 
Posts: n/a

re: Array Remove


There is no native PHP process to do the equivalent of "array_remove()"
as of yet. Your best would be to slap together a function using
unset($array['element']), as I've had to do.

Bear in mind that in future additions to PHP an "array_remove()"
function might someday exists, it's best that when you create it to
wrap the function around a "function_exists()" conditional block:

if (!function_exists('array_remove')) {
function array_remove($element, &$array, $willSearchKeys) {
// EXERCISE LEFT TO THE STUDENT!
}
}

Phil

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multi Array? Mtek answers 8 June 2nd, 2008 11:37 AM
Help!! How do I delete part of an array? JLC answers 3 April 11th, 2007 05:20 PM
Multidimensional array remove values Chuy08 answers 4 February 13th, 2007 05:15 PM
Remove array items iteratively Derek Basch answers 7 July 23rd, 2005 07:11 PM