How can PHP arrays be utilized for more efficient file handling compared to multiple fopen() calls?

When handling multiple files in PHP, using arrays can be more efficient than making multiple fopen() calls because it allows you to store file handles in a structured way and easily iterate over them. By using arrays, you can reduce the number of file operations and improve code readability.

// Example of utilizing PHP arrays for more efficient file handling

$files = ['file1.txt', 'file2.txt', 'file3.txt'];
$fileHandles = [];

// Open all files and store their handles in an array
foreach ($files as $file) {
    $fileHandles[$file] = fopen($file, 'r');
}

// Read from each file handle
foreach ($fileHandles as $file => $handle) {
    while (!feof($handle)) {
        echo fgets($handle) . "<br>";
    }
}

// Close all file handles
foreach ($fileHandles as $handle) {
    fclose($handle);
}