Edit report at https://bugs.php.net/bug.php?id=70937&edit=1
ID: 70937
Updated by: laruence@php.net
Reported by: David dot Gausmann at measX dot com
Summary: natsort/natcasesort change key data type
-Status: Open
+Status: Not a bug
Type: Bug
Package: Arrays related
PHP Version: 5.6.15
Block user comment: N
Private report: N
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2015-11-18 18:23:29] nhojohl at gmail dot com
This is because natsort and natcasesort maintain the key association (it does not re-key the array).
An array must have sequential keys in order for json_encode to encode it as a natural array,
otherwise it converts the array to an associative array (an object) and maintains the key value
association.
If you use array_values on the resulting array after sorting it; it'l resolve your issue
(relevant reference: http://php.net/manual/en/function.natcasesort.php#32610)
Example:
$array = array('b', 'a', 'c');
natsort($array);
$array = array_values($array);
echo "After natsort:<br>\r\n", json_encode($array), "<br>\r\n";
$array = array('b', 'a', 'c');
natcasesort($array);
$array = array_values($array);
echo "After natcasesort:<br>\r\n", json_encode($array), "<br>\r\n";
------------------------------------------------------------------------
[2015-11-18 13:57:16] David dot Gausmann at measX dot com
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 this bug report at https://bugs.php.net/bug.php?id=70937&edit=1