These examples demonstrate the effectiveness of using loops to modify array elements in PHP. The foreach loop is particularly useful for iterating through associative arrays, while for loops are suitable for indexed arrays. By understanding how to manipulate arrays using loops, you can enhance your PHP programming skills and tackle a wide range of data processing tasks.
Example 1: Modifying Student Grades
Suppose you have an array of students with their respective grades.
To increase each student's grade by 5 points, you can use a foreach loop.
The &$student syntax indicates that the student variable should be passed by reference, meaning that changes made to $student within the loop will directly affect the original array element.
$students = [ ["name" => "Alice", "grade" => 85], ["name" => "Bob", "grade" => 72], ["name" => "Charlie", "grade" => 91], ]; // To increase each student's grade by 5 points, you can use a foreach loop. foreach ($students as &$student) { $student["grade"] += 5; } /* To avoid accidentally changing the value later, it’s recommended to unset the temporary variable after the loop like this: */ unset($student); //Print $students Array print_r($students);
Example 2: Converting Teacher Names to Uppercase
Consider an array of teachers and their names.
To convert all teacher names to uppercase, you can use a for loop.
The for loop iterates through the teachers array, accessing each element using the index $i. The strtoupper() function is used to convert the current teacher's name to uppercase and assign it back to the array.
$teachers = [ "Mr. Smith", "Ms. Jones", "Dr. Brown", ]; //To convert all teacher names to uppercase, you can use a for loop. for ($i = 0; $i < count($teachers); $i++) { $teachers[$i] = strtoupper($teachers[$i]); } print_r($teachers);
Example 3: Adding Default Values to Car Mileage
Imagine an array of cars with their mileage information
To set the mileage to 0 for cars with no mileage information, you can use a foreach loop with an if-else statement.
The isset() function checks if the mileage key exists in the current car array element. If not, the mileage value is set to 0. The &$car syntax ensures that the modified car object is updated in the original array.
$cars = [ ["make" => "Toyota", "model" => "Camry", "mileage" => 25000], ["make" => "Honda", "model" => "Civic", "mileage" => 18000], ["make" => "Ford", "model" => "F-150", "mileage" => 32000], ]; /* To set the mileage to 0 for cars with no mileage information, you can use a foreach loop with an if-else statement. */ foreach ($cars as &$car) { if (isset($car["mileage"])) { $car["mileage"] = 0; } } print_r($cars);
Example 4: Assigning Teacher Subjects
Suppose you have an array of teachers and their respective subjects
To add a new subject to each teacher's list, you can use a nested foreach loop.
Here's a breakdown of the loop:
-
foreach ($teachers as $teacher => &$subjects): This iterates through the $teachers array, assigning the teacher's name to $teacher and the corresponding array of subjects to $subjects. Again, the & symbol ensures modifications to $subjects affect the original array.
-
$subjects[] = 'Computer Science';: This statement adds the string 'Computer Science' to the end of the current teacher's list of subjects.
$teachers = [ 'Mr. Smith' => ['Math', 'Science'], 'Ms. Johnson' => ['English', 'History'], 'Mr. Brown' => ['Art', 'Music'], ]; /* To add a new subject to each teacher's list, you can use a nested foreach loop. */ foreach ($teachers as $teacher => &$subjects) { $subjects[] = 'Computer Science'; } print_r($teachers);