Bug #67182 [Opn->Nab]: asort() broken with boolean values.

From: Date: Fri, 02 May 2014 17:28:17 +0000
Subject: Bug #67182 [Opn->Nab]: asort() broken with boolean values.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-185585@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=67182&edit=1

 ID:                 67182
 Updated by:         requinix@php.net
 Reported by:        php at liambowers dot co dot uk
 Summary:            asort() broken with boolean values.
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            Arrays related
 Operating System:   Windows 7
 PHP Version:        5.4.28
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2014-05-02 15:55:51] 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
)


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=67182&edit=1


Thread (2 messages)

« previous php.bugs (#185585) next »