dooo61733 2016-05-26 09:30
浏览 161
已采纳

JSON_ERROR_RECURSION对json_encode的意义是什么?

I get this error on PHP7 every about 100 requests for some odd reason and I cannot get rid of it until I restart the fpm demon, however, the real problem is I cannot explain what the error is to start diagnosing this.

I took a look at the documentation http://php.net/manual/en/function.json-last-error.php which was not very useful and there does not seem to be any real links hanging about.

I know this error is not actually related to recursion depth (JSON_ERROR_DEPTH) so what does this error actually mean?

This is the var_dump() of the array that is failing:

 array (
'ns' => 'user',
'where' => '{"_id":"MongoDB\\\\BSON\\\\ObjectID(5505a4f647ac1824618b4567)","status":10}',
'projection' =>
array (
),
'sort' =>
array (
),
'limit' => NULL,
'skip' => NULL,
)
  • 写回答

2条回答 默认 最新

  • duannao3402 2016-05-26 09:39
    关注

    JSON_ERROR_RECURSION indicates that the data passed to json_encode() contains one or more recursive references.

    $data = array();
    $data['foo'] = &$data; // <-- recursive reference here
    
    var_dump(json_encode($data)); // bool(false)
    var_dump(json_last_error_msg()); // string(18) "Recursion detected"
    var_dump(json_last_error() === JSON_ERROR_RECURSION); // bool(true)
    

    Code like this will work though:

    $data['foo'] = 'hello';
    $data['bar'] = &$data['foo'];
    

    But not like this (another recursive reference):

    $data['foo'] = [1, 2, 3];
    $data['foo'][] = &$data['foo'];
    

    Recursive reference means that the reference points to a variable that in turn contains the same reference again.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 脱敏项目合作,ner需求合作
  • ¥15 脱敏项目合作,ner需求合作
  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密