What are some best practices for optimizing array manipulation in PHP?

When working with arrays in PHP, it's important to optimize array manipulation for better performance. Some best practices include using built-in array functions instead of loops for common operations like filtering, mapping, and reducing arrays. Additionally, leveraging associative arrays for key-value pair data can improve lookup times. Avoid unnecessary array copying and use array references when possible to reduce memory usage.

// Example of optimizing array manipulation in PHP

// Using array_filter() instead of a loop to filter an array
$numbers = [1, 2, 3, 4, 5];
$filteredNumbers = array_filter($numbers, function($num) {
    return $num % 2 == 0;
});

// Using associative arrays for key-value pair data
$user = [
    'name' => 'John Doe',
    'age' => 30,
    'email' => 'john.doe@example.com'
];

// Avoid unnecessary array copying and use array references
$originalArray = [1, 2, 3, 4, 5];
$referenceArray = &$originalArray;