What potential pitfalls should be considered when using json_encode with nested arrays in PHP?

When using json_encode with nested arrays in PHP, potential pitfalls to consider include ensuring proper encoding of special characters, handling recursive arrays that could cause infinite loops, and potential performance issues with deeply nested arrays. To avoid these pitfalls, you can use the JSON_UNESCAPED_UNICODE flag to prevent special characters from being escaped, implement a recursive function to handle nested arrays, and optimize your code to avoid excessive nesting.

$data = [
    'key1' => 'value1',
    'key2' => [
        'nested_key1' => 'nested_value1',
        'nested_key2' => [
            'deeply_nested_key1' => 'deeply_nested_value1'
        ]
    ]
];

$json = json_encode($data, JSON_UNESCAPED_UNICODE);
echo $json;