[PHP] removing all duplicate values from an array

2003-10-30 Thread Allowee
Hi,

I'm looking for a function that does almost the same as array_unique()

But it must also delete the other duplicate entry.

sample to explain:

array(
'a' = 'one',
'b' = 'one',
'c' = 'zero',
'd' = 'two'
);

I now want the result to be:

array(
'c' = 'zero',
'd' = 'two'
);

This way I will be able to do an array_diff and then see all the duplicate 
entries listed.

this is to find dup. ip address for a hosting system,
displaying both accounts will make it easy to trace the amount of accounts 1 
ip curruntly has.


Anybody know how to do this?

Thanks,
Allowee

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] removing all duplicate values from an array

2003-10-30 Thread Mike Migurski
I'm looking for a function that does almost the same as array_unique()

But it must also delete the other duplicate entry.
snip

Untested pseudo-PHP follows -

$encountered_elements = array();
foreach($original_array as $key = $val)
if(in_array($val, $encountered_elements)) {
unset($original_array[$key]);
} else {
$encountered_elements[$key] = $val;
}

-
michal migurski- contact info and pgp key:
sf/cahttp://mike.teczno.com/contact.html

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] removing all duplicate values from an array

2003-10-30 Thread Kevin Stone
Or... you could use array_count_values() and loop through the array it
produces to find out how many of each duplicate value exists in the
original array.

http://www.php.net/manual/en/function.array-count-values.php

Kevin


-Original Message-
From: Allowee [mailto:[EMAIL PROTECTED] 
Sent: Thursday, October 30, 2003 10:28 AM
To: [EMAIL PROTECTED]
Subject: [PHP] removing all duplicate values from an array

Hi,

I'm looking for a function that does almost the same as array_unique()

But it must also delete the other duplicate entry.

sample to explain:

array(
'a' = 'one',
'b' = 'one',
'c' = 'zero',
'd' = 'two'
);

I now want the result to be:

array(
'c' = 'zero',
'd' = 'two'
);

This way I will be able to do an array_diff and then see all the
duplicate 
entries listed.

this is to find dup. ip address for a hosting system, displaying both
accounts will make it easy to trace the amount of accounts 1 
ip curruntly has.


Anybody know how to do this?

Thanks,
Allowee

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] removing all duplicate values from an array

2003-10-30 Thread Mike Migurski
I'm looking for a function that does almost the same as array_unique()

But it must also delete the other duplicate entry.
snip

Untested pseudo-PHP follows -

   $encountered_elements = array();
   foreach($original_array as $key = $val)
   if(in_array($val, $encountered_elements)) {
   unset($original_array[$key]);
   } else {
   $encountered_elements[$key] = $val;
   }

Oops, that should have been:

   $encountered_elements = array();
   foreach($original_array as $key = $val)
   if(in_array($val, $encountered_elements)) {
   unset($original_array[$encountered_elements[$val]]);
   } else {
   $encountered_elements[$val] = $key;
   }


-
michal migurski- contact info and pgp key:
sf/cahttp://mike.teczno.com/contact.html

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] removing all duplicate values from an array

2003-10-30 Thread Allowee
On Thursday 30 October 2003 20:37, Mike Migurski wrote:
 I'm looking for a function that does almost the same as array_unique()
 
 But it must also delete the other duplicate entry.
 
 snip
 
 Untested pseudo-PHP follows -
 
  $encountered_elements = array();
  foreach($original_array as $key = $val)
  if(in_array($val, $encountered_elements)) {
  unset($original_array[$key]);
  } else {
  $encountered_elements[$key] = $val;
  }

 Oops, that should have been:

$encountered_elements = array();
foreach($original_array as $key = $val)
if(in_array($val, $encountered_elements)) {
unset($original_array[$encountered_elements[$val]]);
} else {
$encountered_elements[$val] = $key;
}

Hi,

I edited it a bit to match my script and after some tweaks of my own code it 
started working :)

Thanks,

Allowee



 -
 michal migurski- contact info and pgp key:
 sf/cahttp://mike.teczno.com/contact.html

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] removing all duplicate values from an array

2003-10-30 Thread Jeremy Russell


 -Original Message-
 From: Mike Migurski [mailto:[EMAIL PROTECTED]
 Sent: Thursday, October 30, 2003 1:37 PM
 To: Allowee
 Cc: [EMAIL PROTECTED]
 Subject: Re: [PHP] removing all duplicate values from an array
 
 I'm looking for a function that does almost the same as
array_unique()
 
 But it must also delete the other duplicate entry.
 snip
 
 Untested pseudo-PHP follows -
 
  $encountered_elements = array();
  foreach($original_array as $key = $val)
  if(in_array($val, $encountered_elements)) {
  unset($original_array[$key]);
  } else {
  $encountered_elements[$key] = $val;
  }
 
 Oops, that should have been:
 
$encountered_elements = array();
foreach($original_array as $key = $val)
if(in_array($val, $encountered_elements)) {
   unset($original_array[$encountered_elements[$val]]);
} else {
$encountered_elements[$val] = $key;
}
 
 

I was running Allowee's example array..

array(
'a' = 'one',
'b' = 'one',
'c' = 'zero',
'd' = 'two'
);

through this and am having a hind time with then logic...  Could you
explain how the output would be array('c' = 'zero', 'd' = 'two')??

Thanks

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] removing all duplicate values from an array

2003-10-30 Thread Mike Migurski
array(
'a' = 'one',
'b' = 'one',
'c' = 'zero',
'd' = 'two'
);

through this and am having a hind time with then logic...  Could you
explain how the output would be array('c' = 'zero', 'd' = 'two')??

I was being pretty sloppy with the code (should've posted pseudocode to
begin with, my bad) but the general logic is: cycle through each element
of the input array. If the value has not been encountered before, store
the value and key in a secondary array. If the value has been encountered
before (it's present in the secondary array), remove it from the input
array, and use the the stored key/value in the secondary array to unset
its first instance in the input array, too.

-
michal migurski- contact info and pgp key:
sf/cahttp://mike.teczno.com/contact.html

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php