Bug #70937 [NEW]: natsort/natcasesort change key data type
From: David dot Gausmann at measX dot com
Operating system:
PHP version: 5.6.15
Package: Arrays related
Bug Type: Bug
Bug description:natsort/natcasesort change key data type
Description:
------------
I am using php to transfer arrays via JSON.
After using natsort/natcasesort for sorting the resulting data aren't
JSON arrays anymore. This happens because natsort/natcasesort are
changing the datatype of the array keys from int to string (but only
when the data becomes sorted; otherwise the key data type won't
change).
A workaround is it to use usort with strnatcmp/strnatcasecmp as second
parameter.
Test script:
---------------
<?php
$array = array('b', 'a', 'c');
echo "After creation:<br>\r\n", json_encode($array), "<br>\r\n";
$array = array('b', 'a', 'c');
sort($array);
echo "After sort:<br>\r\n", json_encode($array), "<br>\r\n";
$array = array('b', 'a', 'c');
rsort($array);
echo "After rsort:<br>\r\n", json_encode($array), "<br>\r\n";
$array = array('b', 'a', 'c');
natsort($array);
echo "After natsort:<br>\r\n", json_encode($array), "<br>\r\n";
$array = array('b', 'a', 'c');
natcasesort($array);
echo "After natcasesort:<br>\r\n", json_encode($array), "<br>\r\n";
$array = array('b', 'a', 'c');
usort($array, 'strnatcmp');
echo "After usort(strnatcmp):<br>\r\n", json_encode($array),
"<br>\r\n";
?>
Expected result:
----------------
In all used sort functions the result should be an array like
["a","b","c"].
Actual result:
--------------
Instead natsort/natcasesort return
{"1":"a","0":"b","2":"c"} in JSON
syntax.
Only the other sort functions return an array like ["a","b","c"].
--
Edit bug report at https://bugs.php.net/bug.php?id=70937&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=70937&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=70937&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=70937&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=70937&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=70937&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=70937&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=70937&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=70937&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=70937&r=support
Expected behavior: https://bugs.php.net/fix.php?id=70937&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=70937&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=70937&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=70937&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70937&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=70937&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=70937&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=70937&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=70937&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=70937&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=70937&r=mysqlcfg
Thread (4 messages)
- David dot Gausmann at measX dot com