PHP: How to encode infinity or NaN numbers to JSON?

后端 未结 5 552
情话喂你
情话喂你 2020-12-11 14:53

Apparently, infinity and NaN are not a part of JSON specification, so this PHP code:

$numbers = array();
$numbers [\'positive_infinity\'] = +INF;
$numbers [\         


        
5条回答
  •  隐瞒了意图╮
    2020-12-11 15:24

    As an update to readers of this question for newer versions of PHP >= 5.5.0, to get INF or NAN values from json_encode to be encoded as 0 rather than json_encode failing to output at all, add the JSON_PARTIAL_OUTPUT_ON_ERROR option.

    As an example: json_encode($data, JSON_NUMERIC_CHECK | JSON_PARTIAL_OUTPUT_ON_ERROR);

提交回复
热议问题