Doc #63129 [Opn->Csd]: array_unique doesn't sort
Edit report at https://bugs.php.net/bug.php?id=63129&edit=1
ID: 63129
Updated by: yohgaki@php.net
Reported by: luckylefti at web dot de
Summary: array_unique doesn't sort
-Status: Open
+Status: Closed
Type: Documentation Problem
Package: Arrays related
Operating System: Mac OS
PHP Version: Irrelevant
-Assigned To:
+Assigned To: yohgaki
Block user comment: N
Private report: N
New Comment:
http://jp1.php.net/function.array-unique#refsect1-function.array-unique-
description
Sort flag is available since 5.2.10
Previous Comments:
------------------------------------------------------------------------
[2013-07-05 09:41:45] of dot olivier dot favre at gmail dot com
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.
------------------------------------------------------------------------
[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
Thread (3 messages)