How can the use of arrays in PHP improve the efficiency and readability of code when dealing with multiple data sets?
Using arrays in PHP can improve efficiency and readability when dealing with multiple data sets by allowing you to store and access data in a structured way. Instead of using individual variables for each piece of data, you can use arrays to group related data together. This makes it easier to loop through data, perform operations on multiple items at once, and pass data between functions.
// Example of using arrays to store and access multiple data sets
$students = [
['name' => 'Alice', 'age' => 20],
['name' => 'Bob', 'age' => 22],
['name' => 'Charlie', 'age' => 21]
];
// Loop through the array of students and display their names
foreach ($students as $student) {
echo $student['name'] . "\n";
}