What are common methods for parsing and accessing data from arrays in PHP, especially when dealing with complex nested arrays?

When dealing with complex nested arrays in PHP, common methods for parsing and accessing data include using loops (such as foreach) to iterate through the array elements, using array functions like array_map or array_walk_recursive for more complex operations, and accessing nested elements by chaining array keys or using multidimensional array syntax. It's important to carefully navigate the array structure and handle different data types appropriately.

// Example of parsing and accessing data from a complex nested array in PHP

$data = [
    'name' => 'John Doe',
    'age' => 30,
    'contact' => [
        'email' => 'john.doe@example.com',
        'phone' => '123-456-7890'
    ],
    'friends' => [
        [
            'name' => 'Jane Smith',
            'age' => 28
        ],
        [
            'name' => 'Tom Brown',
            'age' => 32
        ]
    ]
];

// Accessing top-level elements
echo $data['name']; // Output: John Doe

// Accessing nested elements
echo $data['contact']['email']; // Output: john.doe@example.com

// Iterating through nested arrays
foreach ($data['friends'] as $friend) {
    echo $friend['name'] . ' is ' . $friend['age'] . ' years old.' . PHP_EOL;
}