Array Remove

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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.



Re: Array Remove

thehuby said the following on 30/09/2005 12:29:
Quoted text here. Click to load it

Unset() is the method given in the manual.

In what way is it corrupting your array?


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

Re: Array Remove

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

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) {


Site Timeline