FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » use of array_key_exists() to prevent duplicates?
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: use of array_key_exists() to prevent duplicates? [message #173039 is a reply to message #173038] Fri, 18 March 2011 16:15 Go to previous messageGo to previous message
Captain Paralytic is currently offline  Captain Paralytic
Messages: 204
Registered: September 2010
Karma:
Senior Member
On Mar 18, 3:06 pm, William Gill <nos...@domain.invalid> wrote:
> On 3/18/2011 6:05 AM, Captain Paralytic wrote:
>
>
>
>
>
>
>
>> On Mar 17, 5:09 pm, William Gill<nos...@domain.invalid>  wrote:
>>> On 3/17/2011 11:58 AM, Captain Paralytic wrote:
>
>>>> Alternative 1) Just assign the values to an associative array with the
>>>> serial number as the key. At the end a foreach will produce a list of
>>>> unique values. No need for array_key_exists() at all.
>
>>> Is this any more efficient than if (array_key_exists()) ?
>>> On the minus side, if I edit or modify the memo, it will be overwritten
>>> using this alternative.  I could avoid this, but it seems unnecessarily
>>> complicated.
>> Not complicated at all. My original suggestion keeps the last copy. If
>> they are "duplicates" then they will all be the same. If instead you
>> want the first copy you just add an if test as in
>> if(!isset($myarray[$mykey]))
>>    $myarray[$mykey] = $myvalue;
>
> Yes, after I spoke I realized that it didn't have to be too complicated,
> but I still have to ask: is there any advantage to testing for isset()
> as opposed to testing for array_key_exists() (besides the treatment of
> NULL values), or are you just posing an equally viable alternative?

Well actually I'm not clear from your OP what you really need. If the
source program produces duplicates of old records as well as new ones,
then provided that the new records come after the old ones, I would
assume that the one you wanted to end up with was the latest one and
so you would not want to use either array_key_exists() or isset().

If you want the first occurrence then all the benchmark tests show
isset() being much more efficient than array_key_exists().
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Php Modal
Next Topic: Pipe the content of a variable to a process
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 28 14:38:12 GMT 2024

Total time taken to generate the page: 0.03904 seconds