php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #21920 array_unique buggy example and BC
Submitted: 2003-01-28 07:59 UTC Modified: 2004-07-27 19:03 UTC
From: corinl at gmx dot de Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 4.3.0 OS:
Private report: No CVE-ID: None
 [2003-01-28 07:59 UTC] corinl at gmx dot de
http://de.php.net/manual/de/function.array-unique.php

the example:
$input = array ("a" => "gr?n", "rot", "b" => "gr?n", "blau", "rot");
$result = array_unique ($input);
print_r($result);

should (and does) print:
Array ( [a] => gr?n [0] => rot [1] => blau ) 

and not:
Array
(
    [b] => gr?n
    [1] => blau
    [2] => rot
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-01-28 08:42 UTC] andrey@php.net
 Look at the View Source of your browser.
Also when using CLI the output is as it is documented.
 [2003-01-28 10:08 UTC] corinl at gmx dot de
hi!

yes, the ourput is correct...but the documentation is wrong, bacause it says the example should output:
Array
(
    [b] => green
    [1] => blue
    [2] => red
)
 [2003-01-28 10:14 UTC] andrey@php.net
 Yes, the output on my machine is this. I use the CLI binary.

c:\apache\php>php-cli a_u.php
Array
(
    [a] => gr?n
    [1] => blau
    [2] => rot
)

c:\apache\php>

Andrey
 [2003-01-28 10:26 UTC] corinl at gmx dot de
okay, so as you can the *keys* are different!
 [2003-01-28 10:31 UTC] betz@php.net
I get:
php -v
PHP 4.3.0 (cli) (built: Jan  8 2003 16:10:13)
Copyright (c) 1997-2002 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2002 Zend Technologies
php test.php
Array
(
    [a] => gr?n
    [0] => rot
    [1] => blau
}

Friedhelm
 [2003-01-28 10:47 UTC] andrey@php.net
The docs : 
[snip]
Note that keys are preserved. array_unique() sorts the values treated as string at first, then will keep the first key encountered for every value, and ignore all following keys. It does not mean that the key of the first related value from the unsorted array will be kept.
[/snip]
I get the users result on Windows(4.3.0-dev), RH 6.2(4.3.0-dev), Debian 3.0 (5.0.0-dev).

The docs should be changed.
 [2003-01-28 11:23 UTC] philip@php.net
Maybe the documented example was created on a PHP 4.0.4 box ;)
 [2003-02-05 02:23 UTC] philip@php.net
Both examples were wrong but have been updated to reflect current (4.3.0) behavior.  Am leaving this open until someone can figure out when (or if) the behavior changed.  Apparently the second example was from 4.0.6 but hmm, this is odd.  Nothing in NEWS reflects a change.

http://cvs.php.net/cvs.php/phpdoc/en/reference/array/functions/array-unique.xml

And a historical reference:
http://cvs.php.net/cvs.php/phpdoc/en/functions/array.xml


 [2004-04-08 22:31 UTC] progcom@php.net
From PHP 4.3.0, array_unique() returns the key of the first related value from the unsorted array.

http://cvs.php.net/diff.php/php-src/ext/standard/array.c?r1=1.167&r2=1.168&ty=h

In addition, prior to PHP 4.0.5 used REGULAR SORT, not STRING SORT, and PHP 4.0.4 also compared type (broken behavior)

http://cvs.php.net/diff.php/php-src/ext/standard/array.c?r1=1.76&r2=1.77&ty=h
http://cvs.php.net/diff.php/php-src/ext/standard/array.c?r1=1.86&r2=1.87&ty=h
 [2004-07-27 19:03 UTC] vrana@php.net
PHP 4.3.0 is old enough so there is no need to document behavior prior to it.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 17 00:01:34 2024 UTC