php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66330 array_unique() sort problems
Submitted: 2013-12-20 14:05 UTC Modified: 2013-12-20 17:36 UTC
From: nomail at nomail dot de Assigned:
Status: Not a bug Package: Arrays related
PHP Version: Irrelevant OS: Windows
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: nomail at nomail dot de
New email:
PHP Version: OS:

 

 [2013-12-20 14:05 UTC] nomail at nomail dot de
Description:
------------
The Sort_Flags are not realy working. Only SORT_NUMERIC and this on a unexpected way, it loose some values.

Your menu to select my phpversion has not my Version includet, so my Version is: 5.4.7



Test script:
---------------
$ar = array('rot','gelb','grün','rot','234fw343','blau','gold');

$ar1 = array_unique($ar);
$ar2 = array_unique($ar,SORT_REGULAR);
$ar3 = array_unique($ar,SORT_NUMERIC);
$ar4 = array_unique($ar,SORT_STRING);
$ar5 = array_unique($ar,SORT_LOCALE_STRING);



print_r($ar1);
print_r($ar2);
print_r($ar3);
print_r($ar4);
print_r($ar5);






Expected result:
----------------
### Output ####

##### NOT EXPECTED, this is the unexpected Output #####

Array
(
    [0] => rot
    [1] => gelb
    [2] => grün
    [4] => 234fw343
    [5] => blau
    [6] => gold
)
Array
(
    [0] => rot
    [1] => gelb
    [2] => grün
    [4] => 234fw343
    [5] => blau
    [6] => gold
)
Array
(
    [0] => rot
    [4] => 234fw343
)
Array
(
    [0] => rot
    [1] => gelb
    [2] => grün
    [4] => 234fw343
    [5] => blau
    [6] => gold
)
Array
(
    [0] => rot
    [1] => gelb
    [2] => grün
    [4] => 234fw343
    [5] => blau
    [6] => gold
)




Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-12-20 17:36 UTC] bwoebi@php.net
-Status: Open +Status: Not a bug
 [2013-12-20 17:36 UTC] bwoebi@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

SORT_NUMERIC actually casts to integer and then compares; strings without numbers in it just are casted to 0 during an int cast.

If you cast the array, it results in array(0, 0, 0, 0, 234, 0, 0);
And when you apply array_unique to that array, you notice that that's correct behaviour.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 16:01:37 2025 UTC