Doc #63129 [Com]: array_unique doesn't sort
| From: | of dot olivier dot favre at gmail dot com | Date: | Fri, 05 Jul 2013 09:41:45 +0000 |
| Subject: | Doc #63129 [Com]: array_unique doesn't sort | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-10048@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=63129&edit=1
ID: 63129
Comment by: of dot olivier dot favre at gmail dot com
Reported by: luckylefti at web dot de
Summary: array_unique doesn't sort
Status: Open
Type: Documentation Problem
Package: Arrays related
Operating System: Mac OS
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
This bug report is invalid.
The doc says that (unstable) sorting is used internally for deduplicating the
array.
It says that the first key-value pair is kept once duplicate values are seen. By
"first" they mean after the (unstable) sort.
Hence it is clear that as the array keeps some key-value pairs, keys are not
reassigned and the result is not sorted.
Use array_sort(array_values(array_unique($test))) for your needs instead.
Note: This is not a workaround, it is *the* way of doing what you need.
Previous Comments:
------------------------------------------------------------------------
[2012-09-21 09:50:39] luckylefti at web dot de
Description:
------------
array_unique doesn't sort the array by default for me.
I've testid it under php 5.3.15 and 5.3.10
---
From manual page: http://www.php.net/function.array-unique#refsect1-function.array-unique-description
---
Test script:
---------------
$test = array("beta","gamma","alpha", "beta");
var_dump(array_unique($test));
Expected result:
----------------
array(3) {
[0]=>
string(5) "alpha"
[1]=>
string(4) "beta"
[2]=>
string(5) "gamma"
}
Actual result:
--------------
array(3) {
[0]=>
string(4) "beta"
[1]=>
string(5) "gamma"
[2]=>
string(5) "alpha"
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=63129&edit=1