php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79426 array_unique bad results
Submitted: 2020-03-28 15:36 UTC Modified: 2020-03-28 23:26 UTC
From: dpfender44 at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 7.4.4 OS: windows 10
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dpfender44 at gmail dot com
New email:
PHP Version: OS:

 

 [2020-03-28 15:36 UTC] dpfender44 at gmail dot com
Description:
------------
The function array_unique produces bad results with an array of simple string values.  Many unset elements result in the new array.  The test program shows good results with my own do_array_unique function and bad results with PHP array_unique.

Test script:
---------------
http://shuffle.djpnet.dyndns.org/shuffle3_php.txt


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-03-28 15:39 UTC] peehaa@php.net
-Status: Open +Status: Not a bug
 [2020-03-28 15:39 UTC] peehaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php
 [2020-03-28 18:28 UTC] dpfender44 at gmail dot com
How is this not a bug? The original array contains string values and the result is not correct.  This is not a complicated situation. Is there a flag that is need to make this simple case work as the documentation implies that it should?  If so, then the documentation should be improved.
 [2020-03-28 22:46 UTC] requinix@php.net
> Note that keys are preserved. If multiple elements compare equal under the given
> sort_flags, then the key and value of the first equal element will be retained.
 [2020-03-28 23:26 UTC] dpfender44 at gmail dot com
In this case there are no keys in the array. It is an array of elements that are just simple strings where each has a numeric index provided by PHP when the element is assigned to the array.  The resulting array also contains many elements that have unset values and produce errors of bad index when attempting access thru the array sequentially with successive numeric indexes.  If the count of elements in the array shows 30 elements, then each element index 0 thru 29 should have a value.  Otherwise, the new array makes no sense.
 [2020-03-28 23:42 UTC] bugreports at gmail dot com
> In this case there are no keys in the array

there is nothing like an array without keys in PHP

> each has a numeric index provided by PHP when the element is assigned

so the array has keys as *every* array has by defintion because hash tables are impossible without

php > print_r(['a', 'b', 'c']);
Array
(
    [0] => a
    [1] => b
    [2] => c
)

> If the count of elements in the array shows 30 elements, 
> then each element index 0 thru 29 should have a value.  

and here we are back at "Note that keys are preserved"

> Otherwise, the new array makes no sense

again: "Note that keys are preserved"
 [2020-03-28 23:46 UTC] bugreports at gmail dot com
normal, expected behavior

php > print_r(['x', 'a', 'x', 'b', 'a', 'b']);
Array
(
    [0] => x
    [1] => a
    [2] => x
    [3] => b
    [4] => a
    [5] => b
)

php > print_r(array_unique(['x', 'a', 'x', 'b', 'a', 'b']));
Array
(
    [0] => x
    [1] => a
    [3] => b
)
 [2020-03-28 23:52 UTC] bugreports at gmail dot com
and to finish that discussion: "then each element index 0 thru 29 should have a value.  Otherwise, the new array makes no sense" is nonsense because in most caes nobody iterates arrays with a for-each loop or care about the numeric keys at all

if you care in a specific usecaes just call array_values(9 and you are done, but don#t insist in the performance penalty doing that behind the scenes all the time even if it don't matter for the code

the language has everything you need, just use it

php > print_r(array_values(array_unique(['x', 'a', 'x', 'b', 'a', 'b'])));
Array
(
    [0] => x
    [1] => a
    [2] => b
)
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Fri Jan 28 06:03:41 2022 UTC