php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #14805 array_unique works opposed to the manual
Submitted: 2002-01-02 12:48 UTC Modified: 2002-06-18 16:36 UTC
Votes:3
Avg. Score:4.3 ± 0.5
Reproduced:2 of 3 (66.7%)
Same Version:2 (100.0%)
Same OS:1 (50.0%)
From: pgerzson at freestart dot hu Assigned: venaas (profile)
Status: Closed Package: Arrays related
PHP Version: 4.1.1 OS: MS Windows 98 PWS 4.0
Private report: No CVE-ID: None
 [2002-01-02 12:48 UTC] pgerzson at freestart dot hu
The manual (recent version from cvs) states that :

" array_unique() will keep the first key encountered for  every value, and ignore all following keys. "

I've tested the two examples in this page and I've found
this statement is not true.

<?php
$input = array (4,"4","3",4,3,"3");
$result = array_unique ($input);
var_dump($result);
?>

output: /* PHP 4.0.6 Win'98 PWS */
array(2) {
  [3]=>
  int(4)
  [4]=>
  int(3)
}

but the manual says it should print:

array(2) {
   [0]=>
   int(4)
   [1]=>
   string(1) "3"
}

As you can recognize the latest keys are preserved
for both value 4 and 3.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-01-02 13:20 UTC] pgerzson at freestart dot hu
I forgot to mention that there may be something wrong with 
array_unique itself. There are three values of 3 considered equal in the example above: 
  2 => "3"(string), 4 => 3(int), 5 => "3" (string)

[manual]
 " Two elements are considered equal if and only if (string)
 " $elem1 === (string) $elem2. In words: when the string
 " representation is the same."

Why does array_unique use the index 4 in this case?
(It's neither the first nor the latest key of value 3)


 [2002-01-09 12:18 UTC] pgerzson at freestart dot hu
I tested the examples with PHP 4.1.1 on Apache 1.3.9 under debian stable. array_unique() does preserve the *first* key 
of every related value in this environment.

Simone Cortesi <cortesi@php.net> stated on phpdoc list:
 " On my PHP Version 4.0.6 on System Windows 95/98 4.10
 " (as it reads with phpinfo), I get...
what I got, but

 " On the same machine using Cygwin and PHP/4.0.8-dev 
 " I get:
the expected result (returning with the first keys).




 [2002-01-16 17:43 UTC] venaas@php.net
array_unique() uses qsort internally. Provided qsort doesn't
change order of items that are equal, array_unique() should
work as documented. Until recently the systems own qsort
was used, giving different behavior on different systems.
With latest PHP (in CVS) we use our own qsort which doesn't
preserve order of equal items either. I'll either get that
qsort changed, or I will write a new slower array_unique()
that doesn't depend on qsort behavior.
 [2002-01-16 18:41 UTC] pgerzson at freestart dot hu
Just one question:
What the main goal to stick to preserving the first key
when array_unique() eliminates multiple occurrences.
I think in most cases it's not so important.
 [2002-06-18 16:36 UTC] venaas@php.net
I've now made it keep the first occurrence. The speed is
about the same and I think this behavior is useful in some
cases. You might want to help test CVS or latest snapshot
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 16 17:01:37 2024 UTC