473,386 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 4298
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: CJM | last post by:
Are DOCTYPE statements case sensitive? I cant remember where, but I'm sure I read that case *does* matter... eg. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"...
13
by: J. J. Cale | last post by:
I have the following. There must be something less cumbersome without push pop. The function parameter obj is the event.srcElement and has the attributes(properties?) picNum and alb pinned to it....
5
by: effendi | last post by:
I wrote a simple script to remove an element of an array but I don't think this is the best way to go about it. I have a list of about five elements seperated by ";" I split the array using...
2
by: bmgz | last post by:
I have written a simple function that validates a form based on the form objects' className attribute. The form basically write a "field required" message next to the form element that is blank(and...
9
by: vbportal | last post by:
Hi, I would like to add BitArrays to an ArrayList and then remove any duplicates - can someone please help me forward. I seem to have (at leaset ;-) )2 problems/lack of understanding (see test...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
1
by: CYBER | last post by:
Hello I'm looking for the fastest way to remove an element from an array so the sequence of keys is not changed. For example when i remove an element from this array $tab = Array('a', 'b',...
7
by: Adam Honek | last post by:
Is there a direct way to remove one entry from a one dimensional array? I keep looking but either my eyes are funny or it isn't there. Must we really create a temp array and copy all but 1 of...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.