What are the potential pitfalls to be aware of when manipulating multidimensional arrays in PHP?
One potential pitfall when manipulating multidimensional arrays in PHP is accidentally overwriting data within nested arrays. To avoid this, you can use array functions like array_push to add elements to nested arrays without overwriting existing data.
// Example of adding elements to a nested array without overwriting existing data
$multiDimArray = [
'first' => [
'name' => 'John',
'age' => 30
],
'second' => [
'name' => 'Jane',
'age' => 25
]
];
// Add a new element to the 'first' nested array
array_push($multiDimArray['first'], 'city', 'New York');
print_r($multiDimArray);