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
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 22:01:27 2024 UTC