What are some best practices for optimizing code efficiency when working with arrays in PHP?

When working with arrays in PHP, it's important to optimize code efficiency to ensure better performance. One way to do this is by using built-in array functions instead of manually iterating through arrays. Additionally, minimizing the number of array operations and avoiding unnecessary array copies can also help improve efficiency.

// Example of optimizing code efficiency when working with arrays in PHP

// Using array_map() instead of manual iteration
$numbers = [1, 2, 3, 4, 5];
$multipliedNumbers = array_map(function($num) {
    return $num * 2;
}, $numbers);

// Avoiding unnecessary array copies
$originalArray = [1, 2, 3];
$modifiedArray = $originalArray; // This creates a copy of the original array
// Instead, modify the original array directly to avoid unnecessary copies
foreach ($originalArray as &$num) {
    $num *= 2;
}
unset($num); // Unset the reference variable