Bug #81556 [NEW]: ksort generate different results with the same keys in two arrays

From: Date: Tue, 26 Oct 2021 15:38:51 +0000
Subject: Bug #81556 [NEW]: ksort generate different results with the same keys in two arrays
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-237387@lists.php.net to get a copy of this message
From:             friedrich dot lucas at os-cillation dot de
Operating system: Ubuntu 18.04.6 LTS
PHP version:      8.0.12
Package:          Unknown/Other Function
Bug Type:         Bug
Bug description:ksort generate different results with the same keys in two arrays

Description:
------------
ksort generate different results with the same keys in two arrays under
php with the version 8 or higher.

This case do only occurs in special conditions: array element count, '0'
for array key must exists.

-----
No specific php setup.

Test script:
---------------
// define arrays with the same keys - different key order ('4' and '@'
are swapped)
$array_at = [
    '_' => '', 'Y' => '', '0' =>
'', '+' => '', 'X' => '', 'W'
=> '',
'1' => '', '3' => '', '@' =>
'', '4' => '', '5' => '', 'P'
=> '', 'O' =>
'', 'N' => '', 'M' => '', 'L'
=> '', 'K' => '',
];
$array_fr = [
     '_' => '', 'Y' => '', '0' =>
'', '+' => '', 'X' => '', 'W'
=> '',
'1' => '', '3' => '', '4' =>
'', '@' => '', '5' => '', 'P'
=> '', 'O' =>
'', 'N' => '', 'M' => '', 'L'
=> '', 'K' => '',
];

ksort($array_at);     // sort by key
ksort($array_fr);     // sort by key

// expects the same key at the sorted array first position
var_dump(array_slice($array_at, 0, 1));  // result: [0 => '''], but only
'0' is defined as a key
var_dump(array_slice($array_fr, 0, 1));  // result: ['+' => '' ]

Expected result:
----------------
The same keysort output from array_at and array_fr.

(correct output with php7.x)
array(1) {
  ["+"]=>
  string(0) ""
}
array(1) {
  ["+"]=>
  string(0) ""
}


Actual result:
--------------
(wrong output with php8.x)
array(1) {
  ["+"]=>
  string(0) ""
}
array(1) {
  [0]=>
  string(0) ""
}


-- 
Edit bug report at https://bugs.php.net/bug.php?id=81556&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=81556&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=81556&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=81556&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=81556&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=81556&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=81556&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=81556&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=81556&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=81556&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=81556&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=81556&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=81556&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=81556&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=81556&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=81556&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=81556&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=81556&r=mysqlcfg


Thread (4 messages)

« previous php.bugs (#237387) next »