What are some common pitfalls when using arrays within arrays in PHP?

One common pitfall when using arrays within arrays in PHP is not properly accessing or manipulating the nested arrays. To avoid this, you should always keep track of the keys and indexes of the nested arrays. Another pitfall is accidentally overwriting values in the nested arrays when trying to update them. To prevent this, make sure to correctly target the specific key you want to modify.

// Example of accessing and updating nested arrays in PHP

// Creating a multidimensional array
$nestedArray = [
    'first' => [
        'name' => 'John',
        'age' => 30
    ],
    'second' => [
        'name' => 'Jane',
        'age' => 25
    ]
];

// Accessing and updating a value in the nested array
$nestedArray['first']['age'] = 35;

// Printing the updated value
echo $nestedArray['first']['age']; // Output: 35