Connecting Tech Pros Worldwide Help | Site Map

Array Remove

thehuby
Guest
 
Posts: n/a
#1: Sep 30 '05
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

Oli Filth
Guest
 
Posts: n/a
#2: Sep 30 '05

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
Chung Leong
Guest
 
Posts: n/a
#3: Sep 30 '05

re: Array Remove


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

thehuby
Guest
 
Posts: n/a
#4: Sep 30 '05

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.

comp.lang.php
Guest
 
Posts: n/a
#5: Sep 30 '05

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