Unexpected Output When Using PHP's `json_encode` with Recursive Arrays in Symfony
Quick question that's been bugging me - I'm working with an scenario with `json_encode` in my Symfony 5.4 project when dealing with recursive arrays. The array I'm trying to encode contains references to itself, and I'm getting the output `null` instead of the expected JSON string. For example, the following code: ```php $data = []; $data['parent'] =& $data; $json = json_encode($data); if ($json === false) { echo 'JSON behavior: ' . json_last_error_msg(); } ``` This outputs `JSON behavior: Maximum stack depth exceeded`, which makes sense due to the circular reference. I've tried using `JSON_PARTIAL_OUTPUT_ON_ERROR` as an option, but it doesn't seem to help with this specific case. I've also looked into using `JsonSerializable` to control the JSON output, but I'm unsure how to handle the recursion properly without losing data or causing infinite loops. Can anyone suggest a way to encode recursive arrays without running into this stack depth scenario? Perhaps there's a design pattern or best practice for dealing with such cases in Symfony? This is part of a larger CLI tool I'm building. Is there a better approach?