What are some best practices for iterating over arrays and creating new arrays in PHP?

When iterating over arrays and creating new arrays in PHP, it is important to use efficient methods to avoid unnecessary loops and improve performance. One common approach is to use array_map() or array_filter() functions to iterate over arrays and apply a callback function to each element. This allows for creating new arrays with transformed or filtered data without manually iterating over each element.

// Example of using array_map() to iterate over an array and create a new array with transformed data
$numbers = [1, 2, 3, 4, 5];
$squaredNumbers = array_map(function($num) {
    return $num * $num;
}, $numbers);

print_r($squaredNumbers);