What is the best practice for dynamically creating and filling arrays in PHP?

When dynamically creating and filling arrays in PHP, it's best practice to use a loop to iterate through the data and add elements to the array. This allows for efficient and scalable code, especially when dealing with large datasets.

// Example of dynamically creating and filling an array in PHP
$data = [1, 2, 3, 4, 5]; // Sample data
$newArray = []; // Initialize an empty array

foreach ($data as $value) {
    $newValue = $value * 2; // Perform some operation on the value
    $newArray[] = $newValue; // Add the new value to the array
}

print_r($newArray); // Output the new array