php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67182 asort() broken with boolean values.
Submitted: 2014-05-02 15:55 UTC Modified: 2014-05-02 17:28 UTC
From: php at liambowers dot co dot uk Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.4.28 OS: Windows 7
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: php at liambowers dot co dot uk
New email:
PHP Version: OS:

 

 [2014-05-02 15:55 UTC] php at liambowers dot co dot uk
Description:
------------
When using asort() with a boolean true value in any other position than the first item in an array, it breaks.

Tried with versions 5.4.28 (Win7), 5.4.27 (Win7), 5.4.12 (Win7) and 5.4.10 (OSX)



Test script:
---------------
$tests = array(
	'test_with_boolean_broken_1' => array('a', true, 'b', 'c'),
	'test_with_boolean_broken_2' => array('a', 'b', 'c', true),
	'test_with_boolean_working_1' => array(true, 'a', 'b', 'c'),
	'test_without_boolean_working' => array('a', 1, 'b', 'c')
);

foreach($tests as $test_name => $test_data)
{
	echo '<h1>' . $test_name . '</h1>';
	echo '<li>data 1 (unsorted):<pre>' . print_r($test_data, true) . '</pre>';

	asort($test_data);
	echo '<li>data 2 (sorted):<pre>' . print_r($test_data, true) . '</pre>';

	asort($test_data);
	echo '<li>data 3 (resorted 1):<pre>' . print_r($test_data, true) . '</pre>';

	asort($test_data);
	echo '<li>data 4 (resorted 2):<pre>' . print_r($test_data, true) . '</pre>';	
}

Expected result:
----------------
When using:

Array
(
    [0] => a
    [1] => 1
    [2] => b
    [3] => c
)

Result should be:

Array
(
    [0] => a
    [2] => b
    [3] => c
    [1] => 1
)

Actual result:
--------------
After asort() #1:
Array
(
    [2] => b
    [3] => c
    [1] => 1
    [0] => a
)
After asort() #2:
Array
(
    [0] => a
    [2] => b
    [3] => c
    [1] => 1
)
After asort() #3:
Array
(
    [0] => a
    [1] => 1
    [2] => b
    [3] => c
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-05-02 17:28 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2014-05-02 17:28 UTC] requinix@php.net
Sorting arrays containing mixed types can lead to unexpected results because the default comparison function may return contradictory decisions. Here, "a" == true and true == "b", however "a" < "b".

Use usort() with your own comparison function that uses consistent logic.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Oct 06 13:00:01 2026 UTC