php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #76971 Changed array append behaviour of array_unique
Submitted: 2018-10-04 13:37 UTC Modified: 2018-10-08 08:56 UTC
From: bjoern dot fischer at dezem dot de Assigned: cmb (profile)
Status: Closed Package: Arrays related
PHP Version: 7.2.10 OS: Linux
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: bjoern dot fischer at dezem dot de
New email:
PHP Version: OS:

 

 [2018-10-04 13:37 UTC] bjoern dot fischer at dezem dot de
Description:
------------
---
From manual page: http://www.php.net/function.array-unique
---

Beginning with PHP-7.2 appending an element to an array, after calling array_unique, changed behaviour.

Before, the internal array-index-pointer was unchanged after calling array_unique, now with PHP 7.2 the array-index-pointer is set to the last remaining array element.

This should be documented.

This can be tested here: https://3v4l.org/IdmUX

Test script:
---------------
<?php

$a = [0, 1, 2, 3, 3, 3];

$b = array_unique($a);

$b[] = 4;

var_dump($b);


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-10-04 17:47 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2018-10-04 17:47 UTC] cmb@php.net
This behavioral change is caused by an optimization which only
affects SORT_STRING mode[1].  Formerly the array has been copied
and then non-unique elements have been deleted (without packing
the array afterwards).  Now a new array is built by adding the
unique elements.  Obiously, this can lead to different numeric
indexes.

[1] <http://git.php.net/?p=php-src.git;a=commit;h=6c2c7a023da4223e41fea0225c51a417fc8eb10d>
 [2018-10-05 07:20 UTC] bjoern dot fischer at dezem dot de
Ok, I get why this changed and it actually is be the behaviour that I would have expected in the first place.

But it should still be documented that the behaviour of this function changed. So that people can know why their code breaks.
 [2018-10-06 11:43 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=345783
Log: Fix #76971: Changed array append behaviour of array_unique
 [2018-10-06 11:44 UTC] cmb@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: cmb
 [2018-10-06 11:44 UTC] cmb@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.
 [2018-10-08 08:56 UTC] bjoern dot fischer at dezem dot de
Thank you. :)
 [2020-02-07 06:05 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=d8d97da74f7e3495d66b09c7a8805aede75c4cfb
Log: Fix #76971: Changed array append behaviour of array_unique
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 12:01:31 2024 UTC