|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #62822 array_unique() - Confusing example
Submitted: 2012-08-15 00:27 UTC Modified: 2012-08-23 16:23 UTC
From: danielklein at airpost dot net Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
43 - 31 = ?
Subscribe to this entry?

 [2012-08-15 00:27 UTC] danielklein at airpost dot net
From manual page:
In the example code int(4) and string("3") are both the first and last versions of those values encountered!
$input = array(4, "4", "3", 4, 3, "3");
               ^       ^^^  ^     ^^^
$result = array_unique($input);

It would be better written as
$input = array(4, "4", "3", "4", 3, 3);
$result = array_unique($input);
or something else that shows it's the _first_ value that determines the type, not the last.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-23 16:23 UTC]
It's neither the first nor last value that determines the type of anything here. 
PHP determines type based on context of 
the value. See for details.

However, types are not being determined here. We're just discovering values and 
whichever value happens to be discovered 
first in the array -- that array key will be preserved.

The implementation of array_unique() is such that it will "keep the first key 
encountered for every value". This is 
already noted in the documentation in the Description section of array_unique at 
the top.

This is also why the resulting output from var_dump() in the examples shows that 
key's 0 and 2 are the remaining results.

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

0 being the offset of the first element in the array, and 2 being the offset of 
the third element in the array.
 [2012-08-23 16:23 UTC]
-Status: Open +Status: Not a bug
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Apr 13 19:01:23 2021 UTC