469,644 Members | 1,961 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,644 developers. It's quick & easy.

quickie for removing an element from an array

Hello,
I have an array that contains numbers. Each element in the array
is guaranteed to be unique. Let's say I have another variable which I
know for certain is in the array, but I don't know the position.
What's the quickest way for removing that element?

I know I could just use a foreach, but I'm sure one of the experts
out there has a better way.

Thanks, - Dave
Jul 17 '05 #1
2 4073
> I have an array that contains numbers. Each element in the array
is guaranteed to be unique. Let's say I have another variable which I
know for certain is in the array, but I don't know the position.
What's the quickest way for removing that element?


http://www.php.net/manual/en/function.array-search.php
Jul 17 '05 #2
D. Alvarado wrote:
Hello,
I have an array that contains numbers. Each element in the array
is guaranteed to be unique. Let's say I have another variable which I
know for certain is in the array, but I don't know the position.
What's the quickest way for removing that element?

I know I could just use a foreach, but I'm sure one of the experts
out there has a better way.

Thanks, - Dave

Hi Dave,

The fastest way to remove an unique "element" in an array requires that
you put it in the array as key. Suppose your "elements" are
product codes, then i would do it like this:

$productCodesAsKeys = array(12 => true, 116 => true); //creates array
with 12 and 116 as keys
$productCodesAsKeys[234] = true; // add 234 as key

now if you want to remove 234, do:
unSet($productCodesAsKeys[234]);

and if you want to know if 234 is in the array:
isSet($productCodesAsKeys[234]);

For large arrays this is MUCH faster then forEach or array_search
because it uses the array's built in indexing technique for associative
arrays (probably hashing).

If you need to run through the array you might do:
while (list($productCode) = each($productCodesAsKeys))

Remark: if the keys are not unique, adding them for the second time will
have no effect on the array.
Warning: using strings and numbers and/or booleans as mixed type keys,
the numbers to which strings like "2" may be converted (i.e. 2) will be
considered as the same key. Same for booleans. null will be considered
the same as "". See ttp://www.php.net/manual/en/language.types.array.php

Warning: in the example i used true as the elements. If you want to use
isSet(), do not use null as (possible) element(s).

Greetings,

Henk Verhoeven,
www.phpPeanuts.org.

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by CJM | last post: by
13 posts views Thread by J. J. Cale | last post: by
5 posts views Thread by effendi | last post: by
2 posts views Thread by bmgz | last post: by
24 posts views Thread by RyanTaylor | last post: by
7 posts views Thread by Adam Honek | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.