What are some common pitfalls to avoid when formatting and managing data output in PHP arrays?

One common pitfall to avoid when formatting and managing data output in PHP arrays is not properly handling nested arrays. When working with nested arrays, it's important to use nested loops or recursive functions to access and display the data correctly.

// Example of properly handling nested arrays in PHP
$data = [
    'name' => 'John Doe',
    'age' => 30,
    'contacts' => [
        'email' => 'john.doe@example.com',
        'phone' => '123-456-7890'
    ]
];

foreach ($data as $key => $value) {
    if (is_array($value)) {
        echo "$key:\n";
        foreach ($value as $subKey => $subValue) {
            echo "  $subKey: $subValue\n";
        }
    } else {
        echo "$key: $value\n";
    }
}