php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #63129 array_unique doesn't sort
Submitted: 2012-09-21 09:50 UTC Modified: 2013-07-31 03:59 UTC
Votes:2
Avg. Score:3.0 ± 2.0
Reproduced:1 of 2 (50.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: luckylefti at web dot de Assigned: yohgaki (profile)
Status: Closed Package: Arrays related
PHP Version: Irrelevant OS: Mac OS
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
43 - 3 = ?
Subscribe to this entry?

 
 [2012-09-21 09:50 UTC] 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"
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-05 09:41 UTC] 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.
 [2013-07-31 03:59 UTC] yohgaki@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: yohgaki
 [2013-07-31 03:59 UTC] yohgaki@php.net
http://jp1.php.net/function.array-unique#refsect1-function.array-unique-
description

Sort flag is available since 5.2.10
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jun 01 19:01:30 2024 UTC