Req #74551 [NEW]: json_encode() assumes that one dimensional array is an object
| From: | jon dot ronnenberg at gmail dot com | Date: | Sat, 06 May 2017 13:20:52 +0000 |
| Subject: | Req #74551 [NEW]: json_encode() assumes that one dimensional array is an object | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-208970@lists.php.net to get a copy of this message | ||
From: jon dot ronnenberg at gmail dot com
Operating system: Linux Ubuntu Mate 16.04
PHP version: 7.0.18
Package: JSON related
Bug Type: Feature/Change Request
Bug description:json_encode() assumes that one dimensional array is an object
Description:
------------
<?php
## json_encode() assumes that one dimensional array is an object ##
# Note, To run the entire bug report in php, just copy/paste the entire
# content into a php file and run php your_file.ph
# Unsetting an element will also remove the keys.
# json_encode() will now assume that this is an object,
# and will encode it as such.
# ref: http:#php.net/manual/en/function.json-encode.php#94157
# Note, that JSON_PRETTY_PRINT has no effect on the behavior
# Example:
$array = [0,1,2];
print_r(json_encode($array, JSON_PRETTY_PRINT));
unset($array[1]);
print_r(json_encode($array, JSON_PRETTY_PRINT));
# The real issue and bug introducing confusing comes with array_filter.
# Example:
$array = [0,1,2,3,4,5,6,7,8,9,10];
$odds = function($d) { return $d % 2; };
print_r(json_encode(array_filter($array, $odds), JSON_PRETTY_PRINT));
# The work-around is to always use array_values, when you know that you
will
# serialize a one dimensional array.
# Example:
$array = [0,1,2,3,4,5,6,7,8,9,10];
$odds = function($d) { return $d % 2; };
print_r(json_encode(array_values(array_filter($array, $odds)),
JSON_PRETTY_PRINT));
# Since the data manipulation is often handled in other layers than the
# serialization, this can lead to subtle and hard to trace bugs.
# Often only found when deployed since this does not happen in all
cases,
# as shown below.
# Example:
$array = [0,1,2,3,4,5,6,7,8,9,10];
$below2 = function($d) { return $d < 2; };
print_r(json_encode(array_filter($array, $below2), JSON_PRETTY_PRINT));
# Changing the behavior of array_filter might introduce breaking change
# while changing how json_encode handles one dimensional arrays should
# not.
# I'm not filing this as a bug, since it has been reported seven years
# ago, but it definitely tangents a bug.
# Seven years ago, simoncpu, reported this behavior in the User
Contributed Notes,
# http://php.net/manual/en/function.json-encode.php#94157
# Another bug report touches on the subject:
# https://bugs.php.net/bug.php?id=45162
# But it's hard to distill what the author expect or believe to be
wrong
# with the behavior.
# This report tries to be concise and has nothing to do with associative
arrays.
?>
Test script:
---------------
# The two print_r statements should output the same:
$array = [0,1,2];
print_r(json_encode($array, JSON_PRETTY_PRINT));
unset($array[1]);
print_r(json_encode($array, JSON_PRETTY_PRINT
Expected result:
----------------
[
0,
1,
2
][
0,
1,
2
]
Actual result:
--------------
[
0,
1,
2
]{
"0": 0,
"2": 2
}
--
Edit bug report at https://bugs.php.net/bug.php?id=74551&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=74551&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=74551&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=74551&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=74551&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=74551&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=74551&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=74551&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=74551&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=74551&r=support
Expected behavior: https://bugs.php.net/fix.php?id=74551&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=74551&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=74551&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=74551&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=74551&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=74551&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=74551&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=74551&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=74551&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=74551&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=74551&r=mysqlcfg