Edit report at https://bugs.php.net/bug.php?id=81556&edit=1
ID: 81556
Comment by: friedrich dot lucas at os-cillation dot de
Reported by: friedrich dot lucas at os-cillation dot de
Summary: ksort generate different results with the same keys
in two arrays
Status: Not a bug
Type: Bug
Package: Arrays related
Operating System: Ubuntu 18.04.6 LTS
PHP Version: 8.0.12
Block user comment: N
Private report: N
New Comment:
1. As with the other sorting functions, ksort will sort according to how PHP's standard
comparison operations work. Mixing numeric values like "0" or "4" with
non-numeric values like "+" or "@" can give unexpected results.
Please double check the bug report. There are no mixed values. All keys was set as strings.
With more or lesser keys the code working. Also with mixed data as string key. Only in this
combination will produce this result.
Previous Comments:
------------------------------------------------------------------------
[2021-10-26 17:20:16] requinix@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
1. As with the other sorting functions, ksort will sort according to how PHP's standard
comparison operations work. Mixing numeric values like "0" or "4" with
non-numeric values like "+" or "@" can give unexpected results.
2. Before PHP 8, the order of elements when they compare equal was undefined.
Use the SORT_STRING flag.
https://www.php.net/manual/en/function.ksort.php
https://3v4l.org/D84lS
------------------------------------------------------------------------
[2021-10-26 15:38:50] friedrich dot lucas at os-cillation dot de
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 this bug report at https://bugs.php.net/bug.php?id=81556&edit=1