Bug #79426 [Com]: array_unique bad results
| From: | bugreports at gmail dot com | Date: | Sat, 28 Mar 2020 23:52:50 +0000 |
| Subject: | Bug #79426 [Com]: array_unique bad results | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-226307@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79426&edit=1
ID: 79426
Comment by: bugreports at gmail dot com
Reported by: dpfender44 at gmail dot com
Summary: array_unique bad results
Status: Not a bug
Type: Bug
Package: Arrays related
Operating System: windows 10
PHP Version: 7.4.4
Block user comment: N
Private report: N
New Comment:
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
)
Previous Comments:
------------------------------------------------------------------------
[2020-03-28 23:46:35] 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:42:45] 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:26:53] 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 22:46:29] 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 18:28:18] 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.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=79426
--
Edit this bug report at https://bugs.php.net/bug.php?id=79426&edit=1