How can one efficiently create a new array by looping through the length of an existing array in PHP?

When creating a new array by looping through the length of an existing array in PHP, it is important to initialize an empty array to store the new values. You can then use a for loop to iterate through the existing array and add elements to the new array as needed. This approach allows you to efficiently create a new array based on the elements of the existing array.

// Existing array
$existingArray = [1, 2, 3, 4, 5];

// Initialize an empty array to store new values
$newArray = [];

// Loop through the existing array and add elements to the new array
for ($i = 0; $i < count($existingArray); $i++) {
    $newArray[] = $existingArray[$i] * 2; // Example operation
}

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