How can PHP developers optimize their code when working with arrays to improve readability and maintainability?
PHP developers can optimize their code when working with arrays by using built-in array functions like array_map, array_filter, and array_reduce to perform common operations on arrays without the need for loops. This can improve readability and maintainability by reducing the amount of code and making it more concise and easier to understand.
// Example of using array_map to transform an array
$numbers = [1, 2, 3, 4, 5];
$squaredNumbers = array_map(function($num) {
return $num * $num;
}, $numbers);
print_r($squaredNumbers);