Array Remove

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
thehuby [ Fr, 30 September 2005 13:29 ] [ ID #991966 ]

Re: Array Remove

thehuby said the following on 30/09/2005 12:29:
> 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);
>

Unset() is the method given in the manual.

In what way is it corrupting your array?


--
Oli
Oli Filth [ Fr, 30 September 2005 14:25 ] [ ID #991967 ]

Re: Array Remove

What's wrong with using array_splice($array, $index, 1)?
Chung Leong [ Fr, 30 September 2005 15:39 ] [ ID #991971 ]

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.
thehuby [ Fr, 30 September 2005 15:41 ] [ ID #991972 ]

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
phillip.s.powell [ Fr, 30 September 2005 17:15 ] [ ID #991979 ]
PHP » comp.lang.php » Array Remove

Vorheriges Thema: Multithreading/Parallel Processing with PHP
Nächstes Thema: Image display that will change for every 5 sec.